Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Backbone.js is a library that doesn't really enforce how you'd like to structure your App (Or the relationship between your Controller, Model, Router, etc.)</p> <p>Below is one of the many ways to do it.</p> <p>Couple highlights:</p> <ol> <li>Router kicks off the fetch process.</li> <li>When model has been fetched, Router then asks the View to render data (Instead of having the View listening to change events from the Model.)</li> </ol> <p>This assumes that PlayerCardView is a "read only" view, as the View doesn't listen to change events from the Model. Depending on your use case, this might not be desirable, so it ultimately depends on how you'd like to handle it.</p> <p>Here are some sample code:</p> <pre><code>(function (export) { var App = export.App = {}; // Stores state/current views of the App App.state = {}; App.state.currentPlayer = null; // Model containing the player App.PlayerModel = Backbone.Model.extend({}); // Single Player View (Assuming you have a collection view for list of players) App.PlayerCardView = Backbone.View.extend({ model: App.PlayerModel, template: _.template('&lt;%= id %&gt;'), render: function(parentEl) { // Render player this.$el.html(this.template(this.model.toJSON())); // Append player view to parent container if (parentEl) { parentEl.append(this.$el); } return this; } // Don't forget to clean up as well! }); // Router App.Router = Backbone.Router.extend({ routes: { 'player/:id': 'showPlayer' }, showPlayer: function(id) { // Unload current player view, if necessary // Construct model var player = App.state.currentPlayer = new App.Player({ id: id }); // Pass model to the new player view var view = App.state.currentPlayerView = new App.PlayerCardView({ model: App.state.currentPlayer }); // At this time, you should probably show some loading indicator as you're fetching data from the server // Fetch data player.fetch({ success: function() { // This would be called when data has been fetched from the server. // Render player on screen view.render($('#parentContainerId')); } }); } }); // Initializes the App App.init = function() { // Kick off router App.state.router = new App.Router(); export.Backbone.history.start(); }; })(window); // Start the app! window.App.init(); </code></pre> <p>Gist: <a href="https://gist.github.com/dashk/5770073" rel="nofollow">https://gist.github.com/dashk/5770073</a></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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