Skip to content

Instantly share code, notes, and snippets.

@ColinCampbell
Created September 21, 2011 17:48
Show Gist options
  • Save ColinCampbell/1232785 to your computer and use it in GitHub Desktop.
Save ColinCampbell/1232785 to your computer and use it in GitHub Desktop.
mixins/action_support.js
// ==========================================================================
// Project: SproutCore - JavaScript Application Framework
// Copyright: ©2006-2011 Strobe Inc. and contributors.
// Portions ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
var get = SC.get, set = SC.set, getPath = SC.getPath;
/**
@class
Implements basic target and action support for views.
@author Erich Ocean
@author Colin Campbell ([email protected])
@since SproutCore 1.7
*/
SC.ActionSupport =
/** @scope SC.ActionSupport.prototype */ {
/**
The target object to invoke the action on when fireAction() is called.
If you set this target, the action will be called on the target object
directly when fireAction() is called. If you leave this property set to
null, then the responder chain will be searched for a view that implements
the action.
@type Object
@default null
*/
target: null,
/**
The name of the action you want triggered when fireAction() is called.
This property is used in conjunction with the target property to execute
a method when fireAction() is called.
If you do not set a target, then calling fireAction() will cause the
responder chain to search for a view that implements the action you name
here. If you set a target, then fireAction() will try to call the
method on the target itself.
@type String
@default null
*/
action: null,
/**
Will be sent along with the action to provide the context of the action.
This is an easy way to include information along with the action.
@type Object
@default null
*/
actionContext: null,
/**
Perform the action. If an action paramter is not provided, then
the action defaults to the `action` property.
@param {String} [action] The action to fire.
@returns {Boolean} true if successful
@returns {Boolean} false otherwise
*/
fireAction: function(action) {
var target = this.get('target') || this;
if (action === undefined) { action = this.get('action'); }
if (action) {
if (typeof target === 'string') { target = getPath(target); }
if (typeof target.sendAction === 'function') {
return target.sendAction(action, this, this.get('actionContext'));
} else {
if (typeof action === 'string') { action = target[action]; }
return action.call(target, this, this.get('actionContext'));
}
}
return false;
}
};
// ==========================================================================
// Project: SproutCore Docs
// Copyright: ©2011 Strobe Inc. All rights reserved.
// ©2008-2011 Apple Inc. All rights reserved.
// License: Licensed under MIT license (see license.js)
// ==========================================================================
require('docs/mixins/action_support');
var get = SC.get, set = SC.set;
SC.Button = SC.Button.extend(SC.ActionSupport);
SC.Button.reopen({
mouseUp: function(event) {
if (get(this, 'isActive')) {
this.fireAction();
set(this, 'isActive', false);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment