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