-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Improved closure based actions per RFC #50
- Loading branch information
Showing
9 changed files
with
626 additions
and
131 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
76 changes: 76 additions & 0 deletions
76
packages/ember-routing-htmlbars/lib/keywords/closure-action.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import Stream from "ember-metal/streams/stream"; | ||
import { map } from "ember-metal/array"; | ||
import { | ||
read, | ||
readArray | ||
} from "ember-metal/streams/utils"; | ||
import keys from 'ember-metal/keys'; | ||
import { symbol } from "ember-metal/utils"; | ||
|
||
export const INVOKE = symbol("INVOKE"); | ||
|
||
export default function closureAction(morph, env, scope, params, hash, template, inverse, visitor) { | ||
return new Stream(function() { | ||
map.call(params, this.addDependency, this); | ||
map.call(keys(hash), (item) => { | ||
this.addDependency(item); | ||
}); | ||
|
||
var rawAction = params[0]; | ||
var actionArguments = readArray(params.slice(1, params.length)); | ||
|
||
var target, action, valuePath; | ||
if (rawAction[INVOKE]) { | ||
// on-change={{action (mut name)}} | ||
target = rawAction; | ||
action = rawAction[INVOKE]; | ||
} else { | ||
// on-change={{action setName}} | ||
// element-space actions look to "controller" then target. Here we only | ||
// look to "target". | ||
target = read(scope.self); | ||
action = read(rawAction); | ||
if (typeof action === 'string') { | ||
// on-change={{action 'setName'}} | ||
actionArguments.unshift(action); | ||
if (hash.target) { | ||
// on-change={{action 'setName' target=alternativeComponent}} | ||
target = read(hash.target); | ||
} | ||
action = target.send; | ||
} | ||
} | ||
|
||
if (hash.value) { | ||
// <button on-keypress={{action (mut name) value="which"}} | ||
// on-keypress is not even an Ember feature yet | ||
valuePath = read(hash.value); | ||
} | ||
|
||
return createClosureAction(target, action, valuePath, actionArguments); | ||
}); | ||
} | ||
|
||
function createClosureAction(target, action, valuePath, actionArguments) { | ||
if (actionArguments.length > 0) { | ||
return function() { | ||
var args = actionArguments; | ||
if (arguments.length > 0) { | ||
args = actionArguments.concat(Array.prototype.slice.apply(arguments)); | ||
} | ||
if (valuePath && args.length > 0) { | ||
args[0] = Ember.get(args[0], valuePath); | ||
} | ||
return action.apply(target, args); | ||
}; | ||
} else { | ||
return function() { | ||
var args = arguments; | ||
if (valuePath && args.length > 0) { | ||
args = Array.prototype.slice.apply(args); | ||
args[0] = Ember.get(args[0], valuePath); | ||
} | ||
return action.apply(target, args); | ||
}; | ||
} | ||
} |
Oops, something went wrong.