0
我是Backbone的新手,目前正試圖讓我的個人模型可以使用Save()
方法。我目前有一切工作,但是當我在單個項目上調用Save()
時,它告訴我該方法不存在。有任何想法嗎?提前致謝。獲取「對象不支持屬性或方法;保存;」當試圖保存一個Backbone.js模型
代碼:
var Root = this;
//MODELS
var Option = Backbone.Model.extend({});
var BooleanOption = Option.extend({
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
if (this.get("ValueAsString") === "Y") { this.set("IsChecked", true); };
},
IsChecked: false
});
var SelectOption = Option.extend({
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
this.set("Options", this.get("ValidationValue").split(","));
},
Options: []
});
var TextOption = Option.extend({
initialize: function() {
this.constructor.__super__.initialize.apply(this, arguments);
this.set("MaxLength", Number(this.get("ValidationValue").replace("x", "")));
},
MaxLength: null
});
//main collection model
var OptionsCollection = Backbone.Collection.extend({
model: function (attr, options) {
switch (attr.ValidationType) {
case "B":
return new BooleanOption(attr, options);
break;
case "O":
return new SelectOption(attr, options);
break;
case "C":
return new TextOption(attr, options);
break;
default:
return new Option(attr, options);
break;
}
},
urlBase: "http://localhost:40217/Shared/Options.svc/",
url: function() {
return this.urlBase + Root.getParam("ModuleID") + "?true";
}
});
//END MODELS
//VIEWS
var OptionView = Backbone.View.extend({
render: function (eventName) {
}
})
var BooleanOptionView = OptionView.extend({
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function (eventName) {
$("#content").append(this.el);
$(this.el).html(_.template($('#boolean-option-template').html(), this.model));
return this;
},
events: {
"change .chkBox": "test"
},
test: function() {
alert("valueChanged");
}
});
var SelectOptionView = OptionView.extend({
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function (eventName) {
$("#content").append(this.el);
$(this.el).html(_.template($('#select-option-template').html(), this.model));
return this;
},
events: {
"change .selectOption": "test"
},
test: function() {
alert("valueChanged");
}
});
var TextOptionView = OptionView.extend({
initialize: function() {
this.listenTo(this.model, "change", this.render);
},
render: function (eventName) {
$("#content").append(this.el);
$(this.el).html(_.template($('#text-option-template').html(), this.model));
return this;
},
events: {
"change .textOption": "test"
},
test: function() {
alert("valueChanged");
}
});
var MainView = Backbone.View.extend({
render: function (eventName) {
$("#content").append(this.el);
$(this.el).html(_.template($('#main-template').html(), this.model));
_.each(this.model.models, function (opt) {
if (opt.get("ValidationType") === "B") {
new BooleanOptionView({ model: opt }).render();
}
else if (opt.get("ValidationType") === "C") {
new TextOptionView({ model: opt }).render();
}
else if (opt.get("ValidationType") === "O") {
new SelectOptionView({ model: opt }).render();
}
}, this);
return this;
},
events: {
"click .saveBtn": "saveOptions"
},
saveOptions: function() {
_.each(this.model.models, function (mod) {
mod.Save(mod.attributes);
})
}
});
//END VIEWS
$(document).ready(function() {
var oc = new OptionsCollection();
oc.fetch({
success: function() {
$("#content").append(new MainView({model:oc}).render().el);
}
});
});
function getParam(sname) {
var params = location.search.substr(location.search.indexOf("?") + 1);
var sval = "";
params = params.split("&");
// split param and value into individual pieces
for (var i = 0; i < params.length; i++) {
temp = params[i].split("=");
if ([temp[0]] == sname) { sval = temp[1]; }
}
return sval;
}
完美。非常感謝您的支持。現在,當我在角落裏哭泣時,請原諒我..... – CptCrash80