finnsson / pagerjs

The Single Page Application Framework for KnockoutJS
http://pagerjs.com/
MIT License
271 stars 68 forks source link

Documentation for lazy bind view model #182

Closed 2piix closed 9 years ago

2piix commented 10 years ago

Can we have documentation for "lazy bind view model" that does not depend on require.js? Using require.js to inject the code is troubling for many reasons, not least of which is the fact that I have to visit the github repo to find the source code for 'invention.js'. It is also troubling because it obscures the lesson. In particular, I find myself asking "What is that callback for? For Pager? For Knockout? For Require?"

hagabaka commented 9 years ago

It took me a while to figure out this too, but Pager provides the callback, and you're supposed to pass it the viewModel that you just loaded. The callback assigns the passed viewModel to the page's ctx, so you can refer to the viewModel as $data, or refer to its properties by their name directly in the HTML.

I'm using it with $.getJSON like this:

<div data-bind='page: {id: 'one', withOnShow: "$root.loadData(\"one\")"}'>
  <span data-bind='text: $data'>
</div>
viewModel = {
  loadData: function(id) {
    return function(callback) {
       $.getJSON(urlFor(id), function(data) {
          callback(data);
       }
    }
  }
}