使用backbonejs和jquery将模型同步到asp.net服务器
我想看看当在扩展模型上指定 urlRoot
时如何使用 model.save() 方法将模型保存到服务器,但是当我询问时,ajax 请求永远不会触发对于 model.fetch()
或 do model.save()
。注意:我想在不使用 Collection 的情况下希望这是可能的吗?
HTML
<div id="placeholder"></div>
<script type="text/template" id="view_template">
Hello <%= name %>, here is your script <%= script %>
</script>
模型
window["model"] = Backbone.Model.extend({
initialize: function () {
console.log("CREATED");
},
defaults:{
name:"Please enter your name",
script:"Hello World"
},
urlRoot: "index.aspx",
validate: function (attrs) {
},
sync: function (method, model, success, error) {
console.log("SYNCING", arguments);
}
});
视图
window["view"] = Backbone.View.extend({
template:_.template($("#view_template").html()),
initialize: function () {
console.log("INITIALISED VIEW");
this.model.bind("change","render",this);
},
render: function (model) {
console.log("RENDERING");
$(this.el).append(this.template(model));
return this;
}
});
应用程序
$("document").ready(function () {
var myModel = new model({
name: "Stack Overflow",
script: "alert('Hi SO')"
});
var myView = new view({
model: myModel,
el: $("#placeholder")
});
console.log("SAVING");
myModel.save();
console.log("FETCHING");
myModel.fetch();
});
,正如您在我调用 save & 的应用程序中看到的那样fetch
但根据文档,这应该使用 POST -> 触发 ajax 请求保存
& 获取 ->获取
。但它所做的只是将同步函数中的参数记录到控制台中。
I wanted to see how i could save a model to server using model.save()
method when urlRoot
is specified on the extended model, but ajax request never fires when i ask for model.fetch()
or do model.save()
. note: Is hope this is possible without using Collection i suppose?.
HTML
<div id="placeholder"></div>
<script type="text/template" id="view_template">
Hello <%= name %>, here is your script <%= script %>
</script>
Model
window["model"] = Backbone.Model.extend({
initialize: function () {
console.log("CREATED");
},
defaults:{
name:"Please enter your name",
script:"Hello World"
},
urlRoot: "index.aspx",
validate: function (attrs) {
},
sync: function (method, model, success, error) {
console.log("SYNCING", arguments);
}
});
View
window["view"] = Backbone.View.extend({
template:_.template($("#view_template").html()),
initialize: function () {
console.log("INITIALISED VIEW");
this.model.bind("change","render",this);
},
render: function (model) {
console.log("RENDERING");
$(this.el).append(this.template(model));
return this;
}
});
Application
$("document").ready(function () {
var myModel = new model({
name: "Stack Overflow",
script: "alert('Hi SO')"
});
var myView = new view({
model: myModel,
el: $("#placeholder")
});
console.log("SAVING");
myModel.save();
console.log("FETCHING");
myModel.fetch();
});
as you can see in application i call save & fetch
but as per documentation this should fire ajax request with POST -> SAVE
& GET -> FETCH
. But all it does is log's arguments into console in the sync function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您没有看到任何 Ajax 请求的唯一原因是您已经覆盖了 Model.sync 方法。通常,只有当您想要替换 Backbone.sync 中实现的默认 Ajax 同步时,您才会执行此操作。请参阅backbone.js 中的 Model.fetch 中的以下行:
我对您的代码进行了快速测试,如果我重命名您的 Model.sync 方法,我会看到 Ajax 请求。
I think the only reason you are not seeing any Ajax requests is that you have overridden the Model.sync method. Normally you would only do this if you wanted to replace the default Ajax syncing implemented in Backbone.sync. See the following line in Model.fetch in backbone.js:
I did a quick test with your code and I am seeing the Ajax requests if I rename your Model.sync method.