Maximum call stack size exceeded in saving model collection backbone.js?
How to send Array of models to Post API method in backbone?
I am using backbone.js in my mvc application and I have a scenario where I have to pass an array to my Post method in Rest API . I am trying to send an array of models and then calling this.collection.create(model). I am d trying to call Post method as
var ModelArray = Backbone.Model.extend({
urlRoot: av.api.patientInsuranceAddress,
toJSON: function () {
return this.collection.toJSON();
}
});
var modelCollection = new ModelArray(
{
collection: this.collection.models
});
modelCollection.save();
Here e.models is an array of models which I convert to json and call Post method and I want to recieve array of models on Post method like this
public HttpResponseMessage Post(InsuranceAddressViewModel[] model)
{
return null;
}
But I am getting null array in Post method. Is my method of converting array of models to json is fine or will have to do something else. I have tried couple of solutions but couldn't get idea. Please guide
I am trying this solution
When controls comes to toJSON in model. it is giving Uncaught RangeError: Maximum call stack size exceeded. Please guide
I am posting my code here
var PayerAddress = Backbone.Model.extend({
urlRoot: av.api.patientInsuranceAddress,
defaults: {
Address: '',
City: '',
State: '',
Zip: ''
},
toJSON: function () {
return this.collection.toJSON();
}
});
var Payers = Backbone.Collection.extend({
//url: av.api.patientInsuranceAddress,
model: PayerAddress,
});
"Done": {
class: 'btn',
text: "Done",
click: function () {
payerName = self.$el.find('#Payer').val();
var ModelArray = Backbone.Model.extend({
urlRoot: av.api.patientInsuranceAddress,
toJSON: function () {
return this.collection.toJSON();
}
});
var modelCollection = new ModelArray(
{
collection: self.collection.models
});
modelCollection.save();
//self.closedialog();
$(this).dialog("close");
}
}
Are you trying to add models to an existing collection or are you trying to replace your collection with a bunch of models? If the latter use:
this.collection.reset(e.models);
The following this.collection.create(myArrayofModels );
'is used to create a new instance of a model within a collection.' In your above example, you are pasting a string. Backbone then tries to add that string as a model into your collection.
上一篇: 骨干集合与多个模型?