Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Basic Best Practices with CanJS

$
0
0
I've been trying to wrap my head around using CanJS for building a complex, "real" app/product at my company. I have a few basic "best practices" questions. Hopefully these aren't out of place.

Assuming I have this code:

  1. Contact = can.Model({
        findAll: "GET /contacts"
    },{});
  2. Contacts = can.Control({}, {
        init: function() {
            var self = this;
            Contact.findAll({}, function(contacts) {
                self.options.contacts = contacts;
                self.element.html(can.view("contactsList.ejs", contacts));
            });
        }
    });
  3. $(document).ready(function() {
        app = {};
        app.contacts = new Contacts("#contacts");
    });

So my questions are:

  1. Is wiring everything up via the "app" global good or bad? If it's bad, does CanJS offer some other way of accessing the controller instances that are created (i.e. can.controls.Contacts[0] or some such)?
  2. Is there a standard/better way to access a controller's model list/instances than what I've done there (i.e. the "self.options.contacts = contacts;" line from above)? So that later if I need to get that list again, I can just access app.contacts.options.contacts (or write a getter to make that less typing)?
  3. Or perhaps the code above is completely off base...?

Thanks in advance!


Viewing all articles
Browse latest Browse all 3491

Trending Articles