Results 1 to 5 of 5

Thread: Ext.applyOnly

  1. #1
    Sencha User edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    11
      0  

    Default Ext.applyOnly

    I wrote up a blog post about a whitelist Ext.apply and wondered what you guys thought of the idea. Here's the code:

    PHP Code:
    /**
     * Applies only a pre-specified set of properties from one object to another
     * @param {Object} receiver The object to copy the properties to
     * @param {Object} sender The object to copy the properties from
     * @param {Array} whitelist The whitelist of properties to copy (e.g. ['width', 'height'])
     * @return {Object} The receiver object, with any of the whitelisted properties overwritten if they exist in sender
     */
    Ext.applyOnly = function(receiversenderwhitelist) {
      if (
    receiver && sender) {
        
    Ext.each(whitelist || [], function(item) {
          if (
    typeof sender[item] != 'undefined'receiver[item] = sender[item];
        }, 
    this);
      };
      
      return 
    receiver;
    }; 
    Unit tests and usage:

    PHP Code:
    var myObj = {height100width100message'test'};
    var 
    someOtherObj = {height200message'a bad message - will not be copied'};

    Ext.applyOnly(myObjsomeOtherObj, ['height''width']);
    console.log(myObj); // => {height: 200, width: 100, message: 'test'} 
    See my post for further rationale... thoughts?
    Last edited by edspencer; 23 Apr 2009 at 3:18 AM. Reason: bug fix (thanks mabello)
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  2. #2
    Sencha User Animal's Avatar
    Join Date
    Mar 2007
    Location
    Bédoin/Redwood City
    Posts
    30,760
    Vote Rating
    83
      0  
    Longtime Sencha engineer. Now surplus to requirements apparently...

  3. #3
    Sencha User
    Join Date
    Nov 2007
    Location
    London, UK
    Posts
    583
    Vote Rating
    2
      0  

    Default

    I don't see any drawbacks on having that, but you should be careful.
    Just had a look and if a property is like toSave: false, then with your code if (sender[item]) the property won't be copied!
    Example:
    Code:
    var myObj = {height: 100, width: 100, message: 'test'};
    var someOtherObj = {height: 200, toSave: false, message: 'a bad message - will not be copied'};
    
    Ext.applyOnly(myObj, someOtherObj, ['height', 'width', 'toSave']);
    console.log(myObj); // => {height: 200, width: 100, message: 'test'}

  4. #4
    Sencha User edspencer's Avatar
    Join Date
    Jan 2009
    Location
    Palo Alto, California
    Posts
    1,939
    Vote Rating
    11
      0  

    Default

    Quote Originally Posted by mabello View Post
    I don't see any drawbacks on having that, but you should be careful.
    Just had a look and if a property is like toSave: false, then with your code if (sender[item]) the property won't be copied!
    Example:
    Code:
    var myObj = {height: 100, width: 100, message: 'test'};
    var someOtherObj = {height: 200, toSave: false, message: 'a bad message - will not be copied'};
    
    Ext.applyOnly(myObj, someOtherObj, ['height', 'width', 'toSave']);
    console.log(myObj); // => {height: 200, width: 100, message: 'test'}  but toSave is not there
    Ah yes you're right, thanks I've updated the code to fix that bug.

    Damn I didn't see that creep in. Good to know though, but I like my name better :p
    Ext JS Senior Software Architect
    Personal Blog: http://edspencer.net
    Twitter: http://twitter.com/edspencer
    Github: http://github.com/edspencer

  5. #5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •