The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Ext.decorate

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Edward Spencer

Posts: 148
Nickname: edspencer
Registered: Aug, 2008

Edward Spencer is a Ruby/Rails developer and the creator of the ExtJS MVC framework
Ext.decorate Posted: Sep 1, 2009 12:44 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Edward Spencer.
Original Post: Ext.decorate
Feed Title: Ed's Elite blog
Feed URL: http://feeds2.feedburner.com/EdSpencer
Feed Description: Ruby on Rails development, Git issues and ExtJS/JavaScript
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Edward Spencer
Latest Posts From Ed's Elite blog

Advertisement
Sometimes you want to override one of the methods in ExtJS that return a configuration object - let's use Ext.direct.RemotingProvider's getCallData as an example, which looks like this:


getCallData: function(t){
return {
action: t.action,
method: t.method,
data : t.data,
type : 'rpc',
tid : t.tid
};
}

Our aim is to add an 'authentication_token' property to the returned object. You could provide the full config object again in an override, but usually you're overriding to add, remove or change one or two properties and want to leave the rest unmolested. I used to find myself writing a lot of code with this pattern:


//just adds an authentication token to the call data, for context see this forum thread
(function() {
var originalGetCallData = Ext.direct.RemotingProvider.prototype.getCallData;

Ext.override(Ext.direct.RemotingProvider, {
getCallData: function(t) {
var defaults = originalGetCallData.apply(this, arguments);

return Ext.apply(defaults, {
authenticity_token: ''
});
}
})
})();

All we're really doing here is adding 1 config item - an authenticity_token, but it takes a lot of setup code to make that happen. Check out Ext.decorate:


/**
* @param {Function} klass The constructor function of the class to override (e.g. Ext.direct.RemotingProvider)
* @param {String} property The name of the property the function to override is tied to on the klass' prototype
* @param {Object} config An object that is Ext.apply'd to the usual return value of the function before returning
*/
Ext.decorate = function(klass, property, config) {
var original = klass.prototype[property];
override = {};

override[property] = function() {
var value = original.apply(this, arguments);

return Ext.apply(value, config);
};

Ext.override(klass, override);
}

This lets us write the same override like this:


Ext.decorate(Ext.direct.RemotingProvider, 'getCallData', {
authenticity_token: ''
});

Much nicer, we just tell it what we want with no need for unwieldy boilerplate code. This method doesn't actually exist in Ext (though it would be good if something similar did), but you could define it yourself as above to keep such code nice and dry.

Read: Ext.decorate

Topic: Oh My Zsh gets theme support Previous Topic   Next Topic Topic: Lone Star Ruby Conf 2009 Day Two Afternoon Sessions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use