Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organize a multi-application with backbone?
    primarykey
    data
    text
    <p><em>I use a clean backbone without wrappers or frameworks.</em></p> <p>So I create a site to manage by some products. I have two dynamic pages for this: </p> <ul> <li><code>add.php</code></li> <li><code>edit.php</code></li> </ul> <p>These pages are separate backbone applications.</p> <p>In both pages, there is an element displays a product category. Usually it is a drop-down list.</p> <p>Next, I will describe the approximate structure of my scripts. It is simple.</p> <pre><code>js +- models | +- Category.js | ... +- collections | +- Categories.js | ... +- views | +- CategoriesView.js | ... </code></pre> <p><strong>CategoryView.js</strong> contains:</p> <pre><code>App.Views.CategoriesViews = Backbone.View.extend({ template: _.template($('#tpl-categories').html()), events: { 'change select': 'toggle' }, initialize: function () { _.bindAll(this, 'render'); this.collection.on('reset', this.render); }, render: function () { this.$el .html(this.template({ collection: this.collection.toJSON() })); return this; }, toggle: function () { // Some actions here var catId = this.$el.children('select').val(); App.trigger('toggleCategories', catId); } }); </code></pre> <p><strong>Initialization</strong> of the view looks like this:</p> <pre><code>new App.Views.CategoriesViews({ el: $('#select-box-cats'), collection: new App.Collections.Categories }); </code></pre> <p>Since this element is on both pages (add.php and edit.php) it works well for both of them.</p> <p>We assume that the name of the template can be set the same on both pages:</p> <pre><code>&lt;script type="text/template" id="tpl-categories"&gt; </code></pre> <p><em>Although I think it is not a good practice.</em></p> <p><strong>Well, finally my question.</strong></p> <p>What happens if one of these pages need to add an event handler to the view. For example:</p> <pre><code>initialize: function () { _.bindAll(this, 'render', 'action'); this.collection.on('reset', this.render); this.collection.on('request', this.action); }, </code></pre> <p>I added an event to <code>request</code> collection. However, <strong>this event should not be present on another page.</strong></p> <p>What to do? To create a separate view file with the changes to the needs of the page? But it violates the principle of DRY and spawns a lot of client code!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
    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