将数据从主干发布到REST

时间:2013-11-01 08:41:26

标签: javascript rest backbone.js

我刚刚开始使用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按钮时,将view对象从model转到Add to wish list(如上面的示例数据),然后发布它是REST。

1 个答案:

答案 0 :(得分:1)

您需要在愿景中提供您的愿望清单的参考:

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)\/"
                 }
              ]     
              ....
          })
    }
})