Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of Ext.apply versus simply setting values on the target?

Using Ext.js or sencha, what is the point of doing the following:

Ext.apply(app.views, {
        contactsList: new app.views.ContactsList(),
        contactDetail: new app.views.ContactDetail(),
        contactForm: new app.views.ContactForm()
    });

As opposed to this standard javascript:

app.views.contactsList = new app.views.ContactsList();
app.views.contactDetail = new app.views.ContactDetail();
app.views.contactForm = new app.views.ContactForm();

Is there any difference?

like image 848
Chris Avatar asked Mar 11 '11 02:03

Chris


People also ask

What is Ext apply for?

Ext. apply() is used to simplify the copying of many properties from a source to a target object (most of the time the source and target objects have different sets of properties) and it can in addition be used to apply default values (third argument).

What is Ext JS framework?

Ext JS is a JavaScript application framework for building interactive cross-platform web applications using techniques such as Ajax, DHTML and DOM scripting.


3 Answers

Ext.apply is generally more convenient (and possibly more efficient if there are fewer activation chain lookups required, as in your example, though that's a minor point) . There is also a variant Ext.applyIf that only applies members from the source object that do not exist in the target object, which is even more useful as it saves you from a boatload of manual if() checks. That's really useful, for example, when applying defaults to a config object that may already have user or app-defined properties assigned.

A note to future readers who look at the accepted answer: Ext also has Ext.extend which actually means "inherit from" a class, as opposed to Ext.apply[If] which merely merges an object instance into another, or Ext.override which overrides (without subclassing) a class definition. Lots of options, depending on what you need.

like image 75
Brian Moeskau Avatar answered Oct 18 '22 14:10

Brian Moeskau


It's mostly there as a convenience method for code that is accepting an object as an argument, and needs to merge it. Merging objects is a common use case in JavaScript, and this type of helper is implemented by most frameworks. ($.extend in jQuery, Object.extend in Prototype, Object.append in MooTools, etc.)

In your case, there is little difference, other than offering a bit more readable code.

like image 26
Nathan Ostgard Avatar answered Oct 18 '22 12:10

Nathan Ostgard


You need Ext.apply if you use Ajax requests to get the configurations from the server. Because Ajax responses are received later on, after the window is rendered. The second part of your code won't work.

like image 1
ilhan Avatar answered Oct 18 '22 14:10

ilhan