Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to render a view depending on multiple ajax calls in backbone.js?
    primarykey
    data
    text
    <p><br> I have a main view, containing a html form with some empty select tags (-> dropdownlists without elements).<br> For every select tag I have a sub view in my main view. These sub views are responsible for loading their own data.<br> Now I want my main view to render everything at once. How can I recognize that all ajax call by my sub views are done?</p> <p>Here is what I have so far: </p> <pre><code>BookFormView = Backbone.View.extend({ render: function () { this.el.html($.tmpl('book-form-template', this.model)); this.authorView = new AuthorView({ el: $('#authors') }); // #authors is a html select element in book-form-template this.categoryView = new CategoryView({ el: $('#categories') }); // #categories is a html select element in book-form-template this.authorView.el.val(this.model.autherID); this.categoryView.el.val(this.model.categoryID); return this; } }); </code></pre> <p>You can see the render method of my BookFormView. I want to pre-select the right options by the corresponding model-ids. But that won't work, because of the ajax calls in my sub views.</p> <p>My sub views:</p> <pre><code>AuthorView = Backbone.View.extend({ initialize: function (options) { this.render = _.bind(this.render, this); this.collection = new Authors(); // Authors is a backbone.js collection this.collection.bind('refresh', this.render); this.collection.fetch(); }, render: function () { this.el.html($.tmpl('selectoption-template', this.collection)); } }); CategoryView = Backbone.View.extend({ initialize: function (options) { this.render = _.bind(this.render, this); this.collection = new Categories(); // Categories is a backbone.js collection this.collection.bind('refresh', this.render); this.collection.fetch(); }, render: function () { this.el.html($.tmpl('selectoption-template', this.collection)); } }); </code></pre> <p>And finally the controller:</p> <pre><code>BookController = Backbone.Controller.extend({ routes: { 'books': 'list', 'books/:id/edit': 'showEditForm' }, showEditForm: function (id) { var book = new Book({ id: id }); // Book is a backbone.js model book.fetch({ success: function (bookData, resp) { var view = new BookFormView({ model: bookData }); view.render(); } }); } //... }); </code></pre> <p>I know I can pre-load the categories and authors in my controller but that will not solve my problem because their callbacks are still asynchronious.<br> Another solution would be to make authors and categories properties of my book model and render them directly without a custom view. But I prefer to have them separated to re-use them at different locations in my app.</p> <p>Any way to solve this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload