Results 1 to 1 of 1

Thread: Extension: Remap MixedCollection Keys

  1. #1
    Ext User
    Join Date
    Jul 2007
    Vote Rating

    Default Extension: Remap MixedCollection Keys

    This isn't exactly a user-friendly extension, but it's a piece of generic code that I found useful.

    Problem: A user creates a new record client-side, which generates a temporary ID. Later, she uploads the record, and the client receives the record's permanent ID. Complexity, metadata, and processing are all reduced if I can always assume my database and the grid's store use the same IDs. Thus I'd want to update the ID for that record in the associated MixedCollection once it I know of its permanent ID.

    Solution: Place this code in a file or script block to run after ext-full.js and before you instantiate any MixedCollection objects which would use this functionality.

    • All grids use MixedCollection.
    • The extension is wrapped in Ext.onReady. Calls to this function appear to be executed in timestamp-order.
    • I've written these functions such that changing a key will NOT fire any update events.
    • This calls jQuery's version of Extend( intoThis, fromThis, [andThis, ...]). If you're not using jQuery, replace $.Extend with the appropriate substitute from your own library. I'm lazy.

    void MixedCollection.setKey( oldKey, newKey ): maps newKey as the key for the object presently keyed to oldKey.

    void MixedCollection.setKeyAt( idx, newKey ): maps newKey as the key for the object presently located at the idx-th index in the MixedCollection.

    void MixedCollection.setKeyFor( obj, newKey ): maps newKey as the key for obj.

    /* MixedCollection Update Keys API */
    $.extend( Ext.util.MixedCollection.prototype, {
    	setKey: function( oldKey, newKey ){ 
    		// We need to set these properties directly
    		// to avoid firing any "update" events.
    		// items
    		var record =[oldKey]; = newKey; = newKey;
    		// keys
    		var idx = this.keys.indexOf(oldKey);
    		this.keys[idx] = newKey;
    		// map
    		delete[oldKey];[newKey] = record;
    	setKeyAt: function( idx, newKey ){
    		var oldKey = this.getKey(this.itemAt(idx));
    		return this.setKey(oldKey,newKey);
    	setKeyFor: function( obj, newKey ){
    		return this.setKey( this.getKey(obj), newKey );
    }); });
    I thought about adding similar functions to but really, if you need these functions to begin with, you're hacking at the source pretty deep already. I'm sure you can rouse yourself to type instead of ds.setKey.

    If you have any problems with this, let me know. It's been working fine with my app so far.

    Jack: Feel free to add this to the future of Ext if you so desire. (Though if you did, a link would be nice... but it's not like this was complicated.)
    Last edited by david.schoonover; 15 Jul 2007 at 2:07 AM. Reason: Typo, jQuery stuff.

Posting Permissions

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