Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js Model to View Connection

I am a Backbone.js newbie. I was just playing around with it. I would like to know whether the model is related with the View. In the provided todos example, I see in the addOne method, a new View is created and associated with the newly created model and appended.

  window.AppView = Backbone.View.extend({      // view code      addOne: function(todo) {           var view = new TodoView({model: todo});           this.$("#todo-list").append(view.render().el);      }   } 

When I tried to do a similar thing, I got an error saying "bind method cannot be found on undefined".

window.TodoView = Backbone.View.extend({       initialize: function() {           _.bindAll(this, 'render', 'close');           this.model.bind('change', this.render); // I got the error at this place.            this.model.view = this;      } }); 

In order to solve this, I got to pass the newly created model as a param to the view constructor and I got to do this.model = task inorder to associate it.

window.TodoView = Backbone.View.extend({       initialize: function(task) {           _.bindAll(this, 'render', 'close');           this.model = task           this.model.bind('change', this.render);// now there is no error           this.model.view = this;      } });  window.AppView = Backbone.View.extend({      insertTask:function(){         var newTask, newTaskView;         newTask = new Task(JSON.parse(xhr));         Tasks.create(newTask);         newTaskView = new TaskView({ model: newTask });         $("#todo_list").append(newTaskView.render().el);         this.input.val(''); } 

});

But the todos example, do not have something like that. How is the new model associated with the new view implicitly in the todos example?

Thanks

like image 322
felix Avatar asked Jul 14 '11 17:07

felix


People also ask

Is Backbone JS still relevant?

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 El property of backbone JS view?

The Backbone. js View el method defines the element that is used as the view reference. this. el is created from the view's tagName, className, id and attributes properties, if specified.

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.

How does Backbone JS work?

Backbone. js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.


1 Answers

It's not implicit at all, it's explicit in this line right here:

var view = new TodoView({model: todo}); 

This is creating a new TodoView view and setting its model property to the addOne function's only parameter (todo, which is a model).

Whenever a new model is added to the Todos collection, the addOne method is called with the new model as the parameter.

Todos.bind('add', this.addOne); 

Then, in addOne, a new view is created for that model and the relationship is explicity set, via {model: todo}. This is what you're missing from your version of the code, I suspect.

What you appear to be attempting to do is link up the view and the model in the view's init function, and that's fine, but you're on your own if you do that- which means you need to set up the model <-> view relationship yourself (which you have solved by passing the model as a parameter to the view init function).

like image 185
Factor Mystic Avatar answered Sep 26 '22 01:09

Factor Mystic