Backbone model calls the Save method when submitting the mode

Source: Internet
Author: User

Horizon uses the backbone framework, but our background API is to receive only the POST request, the path of the request is/api/, according to Backbone's Official document explanation:

Backbone's Model.save method will determine if the current model object exists in the server, if there is a server, call "Update" (HTTP PUT), if not present, call "create" (http POST), The judgment is based on whether the property ' id ' of the current model exists.

Examples are as follows:

    varUpdateagentv =Modalview.extend ({initialize:function(data) { This. Model =NewUpdateagentm ();  This. SetDefault ();  This. defaults.id = ' edit-agent ';  This. Defaults.header = Translate (' Operations.edit ');  This. Rendermodal ();  This. Model.bind (' Change ', This. Render, This);  This. Model.set ({agents_id:data.project_id});  This. GetAgent (data.project_id); }, GetAgent:function(project_id) {var$ This= This;  This. Ajaxrequest ({action:' Getagentsdetail ', project_id:project_id},function(RES) {$ This. Model.set ({account_bank:res.data.account_bank, Account_name:res.data.account                    _name, Account_number:res.data.account_number, Url:res.data.url,            Logo:res.data.logo, Company:res.data.company,});        }); }, Render:function() {TPL= _.template ($ ("#template-edit-agent"). HTML ());  This. Defaults.body = TPL ( This. Model.tojson ());  This. $el. HTML ( This. Template ( This. defaults)); $('. Modal-dialog '). Draggable ({handle: "Div.modal-header" }); return  This;    },    }); varUpdateagentm =modalmodel.extend ({defaults: {action:' Updateagents ', agents_id:‘‘, Account_bank:‘‘, account_name:‘‘, Account_number:‘‘, URL:‘‘, Logo:‘‘, Company:‘‘,}, Initialize:function() {             This. Validator (); }, Validator:function() {            $("#edit-agent"). Find ("form"). Validate ({rules: {Account_bank : {required:true}, Account_name: {required:true}, Account_number: {required:true}, URL: {required:true}, Logo: {required:true}, Company: {required:true                    },                }            }); }, create:function(Element) {vardata = This. Formserialize (Element);  This. Set (data);  This. Saved (Agentsview); }    });

In the example above, if the Updateagentv initialize This.model.set ({agents_id:data.project_id}) is changed to This.model.set ({id:data.project_id} ), when the model calls the Save method, it will assume that the current model already exists, and the commit data will be used in the Put method.

Backbone model calls the Save method when submitting the mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.