简体   繁体   中英

Save model with params from alloy backbone model

I'm trying to save a model to an endpoint but I just can't figure out how to send the data. I'm using a rest adaptor with Titanium Alloy.

var fav = Alloy.createModel('favourite');
fav.save({item_id: item._id}, {
    success: function(){
        Ti.API.info('Success');
    },
    error: function(){
        Ti.API.info('error');
    }
});

Also tried:

var fav = Alloy.createModel('favourite');
fav.set('item_id', item._id);
fav.save({}, {
    success: function(){
        Ti.API.info('Success');
    },
    error: function(){
        Ti.API.info('error');
    }
});

With debug on I can see the output:

"url": "http://localhost:4000/users/me/favourites", "data": "{}"

How do I send params along with the save?

You have two ways to do that:

The first one is use model.set to set value inside the model then perform save().

setValues:->
   @model.set("someValue":"valueThatIwant")

saveValues:->  
   @save();

If you'd like to specify your callbacks you should do something like this:

saveValues:->  
   @save({}
       success:->

       error:->
   )

The second one is specify the data that you want to post when you're saving. Something like this:

saveValues:->  
    @save({data: {"Value":"valueThatIwant"}}
        success:->

        error:->
    )

The name of the Json file that you're trying o post must be 'data'.

Hope it Helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM