admin管理员组文章数量:1391975
I'm attempting to write a plugin that will present a mostly pre-filled out form with a custom header. I've created a RESTful endpoint to supply and receive the data. And I've created a Model in Backbone.js:
var app={};
app.pilot_id = $("#user_id").val();
app.Pilot = Backbone.Model.extend({
url: POST_SUBMITTER.root + 'pilotdata/v1/pilot/',
sync : function(method, model, options){
return Backbone.sync(method, this, $.extend(options,
{beforeSend : function (xhr) {
xhr.setRequestHeader ('X-WP-NONCE', POST_SUBMITTER.nonce);
}
}))
},
defaults : {
lastName: 'Doe' , firstName: 'John'
},
initialize : function(){
this.fetch({ data: ({id: app.pilot_id})});
}
});
app.pilot = new app.Pilot(); { after the fetch lastName will be 'Smith' and firstName will be 'Sue'
and I've created a form view using BackForm.js
app.PilotForm = Backform.Form.extend({
el: $("#personalInformation"),
events: {
"submit": function(e) {
e.preventDefault();this.model.save( {patch: true})
.done(function(req, status, err)
{
alert( status + ', ' + err);
console.log(status, err);
})
.fail(function(req, status, err){
alert( status + ', ' + err);
});
return false;
}
},
fields: [
{name: "id", label: "Id", control: "uneditable-input"},
{name: "firstName", label: "First Name", control: "input"},
{name: "lastName", label: "Last Name", control: "input"},
{control: "button", label: "Save to server"}
],
});
I've also created a header view(not using BackForm):
app.PilotHeader = Backbone.View.extend({
el: '#header',
template: _.template($('#name-template').html()),
initialize: function(){
this.render();
},
render: function(){
this.$el.html(this.template(this.model.toJSON() ));
return this;
}
});
(template not shown) then calling the views:
new app.PilotHeader({model: app.pilot}).render();
new app.PilotForm({model: app.pilot}).render();
My problem is the header is using the data from _previousAttributes {John, Doe} and the Form is using the correct attributes {Sue, Smith}. I can not get the header to use the correct updated values. What am I missing?
本文标签: rest apirendering view in backbone
版权声明:本文标题:rest api - rendering view in backbone 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744709810a2621060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论