Created
May 17, 2011 06:57
-
-
Save jzajpt/976065 to your computer and use it in GitHub Desktop.
Mixin for status observing - on records, record arrays etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========================================================================== | |
// Project: Billapp.ObserveStatus | |
// Copyright: ©2011 Blueberry.cz Apps s.r.o. | |
// ========================================================================== | |
/*globals Billapp */ | |
/** @namespace | |
@version 0.1 | |
*/ | |
Billapp.ObserveStatus = { | |
//........................................................................ | |
// METHODS | |
/** | |
Observes record status and invokes callback when record reaches | |
given status. | |
@param {Number} status Desired status. | |
@param {Object} target | |
@param {Function|String} method | |
@returns {SC.Object} reciever. | |
*/ | |
observeStatus: function(status, target, method) { | |
var context = { status: status, target: target, method: method }; | |
// normalize method first | |
if (SC.typeOf(method) === SC.T_STRING) method = target[method]; | |
this.addObserver('status', this, '_os_statusDidChange', context); | |
return this; | |
}, | |
//........................................................................ | |
// PRIVATE | |
/** @private */ | |
_os_statusDidChange: function(target, key, value, context) { | |
var recordStatus = target.get(key); | |
if (recordStatus & context.status) { | |
target.removeObserver(key, this, '_os_statusDidChange'); | |
if (context.method) context.method.call(context.target, this, key); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment