Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extend() using underscore vs. backbone [closed]

I know backbone is somewhat depending on underscore and jquery. Is there difference between the two lines below?

app.notifications = _.extend({}, Backbone.Events);

AND

app.notifications  = Backbone.Events.extend({});

If they are NOT the same, how different?

like image 695
HP. Avatar asked Oct 28 '12 01:10

HP.


People also ask

What is backbone view extend?

js extend view is a method under the view class that is used to extend the backbone. js view class in order to create a custom view class. For implementation, first of all, we need to create a custom view class and override the render function if required, also we can specify our own declarative events.

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).

Does backbone need jQuery?

You can use the Backbone. Model without jQuery, but Backbone.

Which function has to be used when you want to trigger the event only once before being removed?

js Event once() The event once method is just like event on method but it causes the bound callback to only fire once before being removed.


1 Answers

Backbone.Events.extend does not exist, so I will refer to Backbone.Model instead.

_.extend(target, mixin1, mixin2) is going to copy properties into the target object

Backbone.Model.extend is going to subclass Backbone.Model basically make a constructor (function) whose prototype has your provided properties. This will allow you to make instances of your new class

var Person = Backbone.Model.extend({name: 'yourName'});
var me = new Person();
alert(me.name);

while _.extend would fail

var Person = _.extend({name: 'yourName'}, Backbone.Model);
var me = new Person();  //error b/c Person is a regular object
alert(me.name);

In short Backbone.Model.extend creates a new constructor (function), while _.extend modifies an existing object;

var modified = {};
alert(modified === _.extend(modified, Backbone.Model)); //true
alert(modified === Backbone.Model.extend(modified)); //false
like image 156
jermel Avatar answered Oct 04 '22 08:10

jermel