Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js singular Models not in collection

Being new to Backbone.js, just wanted to clarify the correct way to go about this simple task.

Developing a web app, almost always, you'll have user accounts, where users can login to your app, and view their personalized data. Generally, their page might show some widgets, their user information (name, avatar, etc).

Now creating a Model per widget and grouping these in a Collection is an easy concept. However, would their user info be stored in a singular Model, which would not be apart of a Collection?

If so, how is the hooked up with the rest of the app? Forgive my ignorance, but I've yet to come across any examples that don't explain Models not using them in Collections (User and Users, Dog, Cat and Animals, etc)

Anyway, sorry for the lengthly explanation. I would be looking for any resources to get me started on making a real-world app, rather than a ToDo app (which is great for the basic concept understanding)

like image 746
crawf Avatar asked Mar 22 '12 12:03

crawf


People also ask

Does anyone use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

What is collections in Backbone JS?

Advertisements. Collections are ordered sets of Models. We just need to extend the backbone's collection class to create our own collection. Any event that is triggered on a model in a collection will also be triggered on the collection directly.

What is the difference between angular and backbone JS?

AngularJS is a framework. BackboneJS is a lightweight easy-to-use library. AngularJS could be a UI system in JS but based on Typescript. BackboneJS could be a UI system in JS based on MVC (Model View Controller) design pattern.


1 Answers

There is not any reason to feel forced to declare a Collection for every Model your App has. It is very common to have Models without Collection associated.

// code simplified and not tested
App.CurrentUser = Backbone.Model.extend({
  url: "http://myapp.com/session.json"
});

App.CurrentUserView = Backbone.View.extend({
  el: "#user-info",

  render: function(){
    this.$el.html( "<h1>" + this.model.get( "name" ) + "</h1>" );
  }
});

var currentUser = new App.CurrentUser();
currentUser.fetch();

var currentUserView = new App.CurrentUserView()({ model: currentUser });
currentUserView.render();
like image 193
fguillen Avatar answered Nov 16 '22 02:11

fguillen