Answer the question
In order to leave comments, you need to log in
How to pass data to template after doing .fetch()?
Good afternoon! I'm writing a simple backbone+jquerymobile application. The question is:
When I receive data from the server, I need to correctly pass this data to the view, where it is passed to the template. Since .fetch() is asynchronous, then simply passing the model to render will not work, so I tried to do the following:
Template:
<script type="text/html" class="template" id="profile-form">
<div data-role="header">
<h3>Step 4</h3>
<a href="#main" data-theme="a">Home</a>
<a href="#logout" data-theme="a">logout</a>
</div>
<div data-role="content">
<h2 class="ui-li-heading"><%= username %></h2>
<p class="ui-li-desc"><strong><%= phone %></strong></p>
</div>
</script>
var UserInfo = Backbone.Model.extend({
url: appConfig.baseURL + "users/",
});
var ProfilePageView = Backbone.View.extend({
events: {
'click #edit': "edit"
},
initialize: function () {
this.template = $.tpl['profile-form'];
},
render: function (eventName) {
var that = this
this.model.fetch({
data: $.param({email: localStorage.getItem('user_email')}),
type: 'POST',
success: function (response) {
$(that.el).html(that.template(response.toJSON()));
}
});
return this;
},
edit: function () {
window.workspace.navigate('#account/edit', { trigger: true});
}
});
profileInfo: function () {
var user = new UserInfo()
this.changePage(new ProfilePageView({ model: user }));
},
Answer the question
In order to leave comments, you need to log in
I would do like this:
initialize: function () {
this.template = $.tpl['profile-form'];
this.model.fetch({
data: $.param({email: localStorage.getItem('user_email')}),
type: 'POST',
}).done(this.render);
},
render: function (eventName) {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question