This is a migrated thread and some comments may be shown as answers.

Example using MVVM and Datasource to create new object

6 Answers 301 Views
MVVM
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 03 Apr 2012, 02:24 AM
Hi,
I am having a few problems and so far are unable to find an example which integrates the basic MVVM regsistration example with a datasource which posts the registration details servier side using Create.
Is anybody able to provide a working example?

var viewModel = kendo.observable({
		assessmentDateSource: new kendo.data.DataSource({
			transport: {
				create: {
					url: crudServiceBaseUrl + "/Assessment/Create",
					type: "POST"
				},
				parameterMap: function (options, operation) {
					if (operation !== "read" && options.models) {
						return {
							models: kendo.stringify(options.models)
						};
					}
					return options;
				}
			}, batch: true,
			schema: {
				model: {
					id: "AssessmentId"
				}
			}
		}),
		firstname: "",
                lastname: "",
                create: function () {
                  //Datasource is empty here do I some how have to push the viewmodel into the datasource?
                  this.assessmentDateSource.sync();
		}
	});

Thanks in advance

6 Answers, 1 is accepted

Sort by
0
Peter Bulloch
Top achievements
Rank 1
answered on 27 May 2012, 01:42 PM
I am in exactly the same quandary as you are. I cannot get the viewModel to initially populate anything!
0
Joshua
Top achievements
Rank 1
answered on 04 Jun 2012, 01:32 PM
If I understand your problem correctly, you just need to make sure your datasource has data before you bind the viewModel. Generally, I try to manually ensure that my datasources are populated in a specific order before binding by using the following trick:

viewModel.dataSource1.fetch(function () {
viewModel.dataSource2.fetch(function () {
k
endo.bind($("content"), viewModel); });
});
0
Peter Bulloch
Top achievements
Rank 1
answered on 04 Jun 2012, 05:59 PM
Joshua - that looks rediculously simple!  I didn't notice the fetch command in the specs so will try that as soon as I can.  If it works I will buy you a beer.  Thanks for the feedback. 
0
Mark
Top achievements
Rank 1
answered on 13 Oct 2015, 10:16 PM
But what if you are "Creating" a new object and not Editing?  I have no need to fetch because I am creating new.  I see all types of examples for Editing, but pretty much NO examples for Create New that doesn't involve a Grid.  Am I missing something here?  I am using Kendo IU, MVVM and Web API.  I'm just trying to get the data POSTed to the Web API by calling .sync from the datasource, but it never hits my API method and I don't receive any errors...anybody got sample code out there???  Thanks
0
Chris
Top achievements
Rank 1
answered on 26 Feb 2018, 08:21 PM

[quote]Mark said:But what if you are "Creating" a new object and not Editing?  I have no need to fetch because I am creating new.  I see all types of examples for Editing, but pretty much NO examples for Create New that doesn't involve a Grid.  Am I missing something here?  I am using Kendo IU, MVVM and Web API.  I'm just trying to get the data POSTed to the Web API by calling .sync from the datasource, but it never hits my API method and I don't receive any errors...anybody got sample code out there???  Thanks[/quote]

I have the exact same problem. Were you able to find a solution?

0
Mark
Top achievements
Rank 1
answered on 26 Feb 2018, 08:54 PM
You have to perform a fetch first.  So I just "fetched" with id=0, sent back a new object to the client that gets bound to the form.  Then everything works...
Tags
MVVM
Asked by
James
Top achievements
Rank 1
Answers by
Peter Bulloch
Top achievements
Rank 1
Joshua
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Chris
Top achievements
Rank 1
Share this question
or