Thanks guys for the reply. I can confirm that I do have this working now. I've iterated to so many times that I can't even remember what the issue may have been but part of it may have had to do with using v1.1.6 which was just in our src and also EJS. I've updated to current build and am now using Mustache. Oh ... and not having "data" as the key for the array in the json ... that made a difference. I guess CAN wants that as the key for the returned array?
We are not yet using can.Component, trying to just get things working basic MVC and work our way in slowly as we learn more. That said, what formula, style do you guys like for structuring your Controllers with view rendering and data accessing within? I've seen a few different patterns, some older more resembling standard success/error callback and some newer utilizing $.Deferred. As in, should I call for my data from my Model, get it, then fire up a Control instance or fire up the Control, then call my data along with view render inside INIT?
So far this makes most sense to me:
- // JSON array
- {
- "data": [
- {
- "key": "val"
- },
- etc ...
- ]
- }
- // Model
- var Things = can.Model.extend({
- findAll: 'GET path/to/file.json || apiUrl';
- });
- // View - external Mustache
- <ul>
- {{#things}}
- <li>{{thing}}
- {{/things}}
- </ul>
- // Controller
- var Ctrl = can.Control.extend({
- init: function( element, options ) {
- Things.findAll().done(function( response ) {
- var frag = can.view( 'path/things.mustache' , { things: response } );
- element.html( frag );
- }.fail( // handle error
- }
- });
- var thingsCtrl = new Cntrl( '.things' );
- // Or would it make more sense to do?
- var Ctrl = can.Control.extend({
- init: function( element, options ) {
- var frag = can.view( options.view, options.data );
- element.html( frag );
- }
- });
- Things.findAll({}).done(function( response ) {
- new Ctrl( '.things' , {
- template: 'path/things.mustache',
- data: { things: response }
- });
- });
Main question is, instantiate the Control and call for the View data with init or call for the data and once that's done instantiate the control. Yes, in either case I could be passing options as in the 2nd.
Last thing, just started messing with can.List ... does Model.List({}) just return the can pseudo array that $.done argument passes on Model.findAll()?
Thanks!