Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If it's working like you want it to then it's correct :) but there are some changes you can make to take advantage of certain features CoffeeScript+Backbone together give you. </p> <p>The first is declaring a class, you could write it this way to be more concise.</p> <pre><code>class Pages extends Backbone.Collection </code></pre> <p>The correct JavaScript will be generated.</p> <p>Another change you could make is to replace all the instances of <code>this.</code> with <code>@</code>. You'd go from typing 5 characters to only 1.</p> <p>Here's a rewrite of what you posted with these changes. Hopefully I didn't miss anything.</p> <pre><code>class Page extends Backbone.Model displayHTML: (model, response) -&gt; $("#content").html(model.get('html')) class Pages extends Backbone.Collection model: Page url: "/pages" current_page: '1-1' initialize: (models, options) -&gt; @fetch(success: @displayFirst) displayFirst: (collection, response) -&gt; model = collection.get(collection.current_page) model.fetch(success: model.displayHTML) nextPage: -&gt; id = "#{new Number(@current_page[2]) + 1}-1" #todo - this will break with 9+ @gotoPage(id) previousPage: -&gt; id = "#{new Number(@current_page[2]) - 1}-1" #todo - this will break with 9+ @gotoPage(id) gotoPage: (id) -&gt; @current_page = id if model = @get(id) model.fetch(success: model.displayHTML) else alert("Eh nooo") class AppView extends Backbone.View el: $('body') events: "click #next-page": "nextPage" "click #previous-page": "previousPage" initialize: -&gt; @pages = new Pages(null, {view:@}) nextPage: -&gt; @pages.nextPage() previousPage: -&gt; @pages.previousPage() appView = new AppView </code></pre>
 

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