我只是从Backbone.js开始,并尝试在我的项目中创建一个简单的Add to wish list
部分。假设我为每个项目都有一个详细页面,并且在页面中包含Add to wish list
按钮。
这是我的模型:
var WishList = Backbone.Model.extend({
url:'http://blahblah.com/Qts/WL.svc/AddWishlist'
});
var _wishList = new WishList();
//here the sample data that I want to get from my view as :
_wishList.set({
"ID" : 0,
"Name" : "",
"CustomerID" : 106,
"Type" : 0,
"LastUpdated" : "\/Date(1383152400000+0700)\/",
"WishlistDetail" : [
{
"ID" : 0,
"WishListID" : 0,
"ItemID" : 22776,
"Quantity" : 2,
"LastUpdated" : "\/Date(1383152400000+0700)\/"
}
]
});
_wishList.save();
当单击项目详细信息页面中的
wishlist
按钮并将其发布到REST时,如何将view
对象从我的model
移到Add to wish list
(如上面的示例数据)。 最佳答案
您需要在ala视图中提供对愿望清单的引用:
var _wishList = new WishList();
var _wishlistView = new WishlistView({
model : _wishList
});
然后,您应该能够从视图中设置模型,如下所示:
var WishlistView = Backbone.View.extend({
el : "#wishlist-el",
events : {
"click" : "handleClick"
},
initialize : function(options){
//Bind the click handler to this view to retain "this"
_.bindAll(this,
"handleClick"
);
},
handleClick : function(e){
this.model.set({
"WishlistDetail" : [
{
"ItemID" : 47984357,
"Quantity" : 50,
"LastUpdated" : "\/Date(1383152400000+0700)\/"
}
]
....
})
}
})