1 // ========================================================================== 2 // Project: The M-Project - Mobile HTML5 Application Framework 3 // Copyright: �2010 M-Way Solutions GmbH. All rights reserved. 4 // Creator: Dominik 5 // Date: 29.10.2010 6 // License: Dual licensed under the MIT or GPL Version 2 licenses. 7 // http://github.com/mwaylabs/The-M-Project/blob/master/MIT-LICENSE 8 // http://github.com/mwaylabs/The-M-Project/blob/master/GPL-LICENSE 9 // ========================================================================== 10 11 m_require('core/utility/logger.js'); 12 13 /** 14 * @class 15 * 16 * The observable knows all observers, mainly views, and pushes updates if necessary. 17 * 18 * @extends M.Object 19 */ 20 M.Observable = M.Object.extend( 21 /** @scope M.Observable.prototype */ { 22 23 /** 24 * The type of this object. 25 * 26 * @type String 27 */ 28 type: 'M.Observable', 29 30 /** 31 * List that contains pairs of an observer with an observable. An observer is tightened to one 32 * observable, but one observable can have multiple observers. 33 * 34 * @type Array|Object 35 */ 36 bindingList: [], 37 38 /** 39 * Attach an observer to an observable. 40 * 41 * @param {String} observer The observer. 42 * @param {String} observable The observable. 43 */ 44 attach: function(observer, observable) { 45 this.bindingList.push({ 46 observer: observer, 47 observable: observable 48 }); 49 }, 50 51 /** 52 * Detach an observer from an observable. 53 * 54 * @param {String} observer The observer. 55 */ 56 detach: function(observer) { 57 /* grep is a jQuery function that finds 58 * elements in an array that satisfy a certain criteria. 59 * It works on a copy so we have to assign the "cleaned" 60 * array to our bindingList. 61 */ 62 this.bindlingList = $.grep(this.bindlingList, function(value, index) { 63 return value.observer !== observer; 64 }); 65 }, 66 67 /** 68 * Notify all observers that observe the property behind 'key'. 69 * 70 * @param {String} key The key of the property that changed. 71 */ 72 notifyObservers: function(key) { 73 _.each(this.bindingList, function(entry){ 74 if(key === entry.observable){ 75 entry.observer.contentDidChange(); 76 } 77 }); 78 } 79 80 });