Nothing
Nothing

Reputation: 2642

Post data from backbone to REST

I'm just starting with Backbone.js, and trying to create a simple Add to wish list part of my project. Assuming that I have a detail page for each item, and in the page contain Add to wish list button.

This is my model :

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();

How can I take the wishlist object from my view to model (like sample data above) when clicking on Add to wish list button in the item detail page, then post it to REST.

Upvotes: 1

Views: 68

Answers (1)

user1026361
user1026361

Reputation: 3657

You'll need to provide a reference to your wishlist to the view ala:

var _wishList = new WishList();
var _wishlistView = new WishlistView({
    model : _wishList
});

You should then be able to set the model from the view like so:

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

Upvotes: 1

Related Questions