Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone change model of View

I am fairly new to Backbone and have the following question:

I have a collection of models.

I have a collection view displaying tabs (with a view for each model in the collection).

I have a view for a model (for the content).

I have a router with routes.

What I am trying to achieve is a functionality like http://jqueryui.com/demos/tabs/

I click on a tab (model of collection) and then want to pass the model to the content view maybe change it and reflect the changes in the collection.

I came up with four solutions:

In the router:

'switchCommunity': function(id) {         // (a) set new model attributes         this.view.community.model.set(communities.get(id));          // (b) replace model         this.view.community.model = communities.get(id);          // (c) a custom function of the view changes its model         this.view.community.changeModel(communities.get(id));          // (d) a new view         this.view.community = new View({             model: communities.get(id)         }) } 

The problem here is

  • (a) does not reflect changes to the model in the collection

  • (b) does not trigger (change) events, because the bind in the initialize function of the view never triggers, because it is a completly new model

  • (c) seems like a hack to me

  • (d) everytime i click on a tab a new view is created (is this a performance issue?)

What is the best pratice here?

like image 769
Riebel Avatar asked Dec 21 '11 15:12

Riebel


People also ask

What is backbone of a model?

Advertisements. Models contain dynamic data and its logic. Logic such as conversions, validations, computed properties and access control fall under the Model category. As it contains all the application data, a model is also called as the heart of JavaScript application.

What is a backbone view?

The Backbone. js Views specify how your data looks like. They represent model's data to the users. They can be used with any JavaScript template library. They handle users input events, bind events and methods, render model and collection and interact with users.

Is backbone a MVC?

Backbone is a JavaScript MVC library and that's a key difference. In the JavaScript MVC context, a framework usually means that you need to configure your code to get things done.

What is backbone in programming?

Backbone. js is a model view controller (MVC) Web application framework that provides structure to JavaScript-heavy applications. This is done by supplying models with custom events and key-value binding, views using declarative event handling and collections with a rich application programming interface (API).


2 Answers

One of your solution is close to be okay :D

Here is what you want:

this.view.community.model.set(communities.get(id).toJSON()); 

And this will trigger model.on("change") as well.

like image 151
zsitro Avatar answered Sep 20 '22 02:09

zsitro


Why do you think (c) is a hack? It seems like a good place to encapsulate the unbinding of the old model, and connecting up the new one.

like image 33
Paul Hoenecke Avatar answered Sep 23 '22 02:09

Paul Hoenecke