-
-
Save vvscode/33f84cabc917fd7ff1f3 to your computer and use it in GitHub Desktop.
Updates for ember 1.12.1
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
var each = Ember.$.each; | |
var merge = Ember.merge; | |
var get = Ember.get; | |
/** | |
* Render component by name | |
* @param componentPath | |
* @param options | |
* | |
* Examples: | |
* | |
{{render-component 'image-component' src="" class="image"}} | |
{{render-component 'pluralize-component' count=ungrouped.content.meta.total single="Object"}} | |
{{#render-component 'btn-component' action="addSection"}} | |
{{#render-component componentName _param='btn-component' action="addSection"}} | |
or | |
{{#render-component 'btn-component' action="addSection"}} | |
Add section | |
{{/render-component}} | |
or even | |
{{#render-component 'render-component' _param='btn-component' action="addSection"}} | |
Add section | |
{{/render-component}} | |
You also can use ___params to define all properties via hash like | |
instead of | |
{{render-component 'pluralize-component' count=ungrouped.content.meta.total single="Object"}} | |
you can use | |
{{render-component 'pluralize-component' ___params=hash}} | |
and hash is | |
hash = { count:ungrouped.content.meta.total, single:"Object"} | |
For cases when we need pass into component not only attributes but param too | |
When we need this? | |
for example you want to render | |
{{componentName paramName someOption=someOptionValue}} | |
You can do | |
{{#render-component 'componentName' someOption=someOptionValue}} | |
BUT! You can't do ( you can't pass more than one argument into component ) | |
{{#render-component 'componentName' paramName someOption=someOptionValue}} | |
so for such cases you can use '_param' ( at line param or at hash property ) | |
{{#render-component 'componentName' _param=paramName someOption=someOptionValue}} | |
or | |
{{#render-component 'componentName' __params=paramsHash}} | |
where paramsHash is | |
paramsHash = { _param:paramName, someOption=someOptionValue } | |
*/ | |
export default function(componentPath, options) { | |
options.hashContexts = options.hashContexts || {}; | |
options.hashTypes = options.hashTypes || {}; | |
var view = options.data.view, | |
resolvePath = function(path) { | |
return view.getStream? view.getStream(path).value(): path; | |
}, | |
getHelper = function(container, helperName) { | |
var atResolvedHelper = (Ember.Handlebars.resolveHelper || function(){})(container, component); | |
var atHelpers = ((Ember.HTMLBars.helpers || Ember.Handlebars.helpers)[helperName] || {}).helperFunction; | |
var atContainer = (container.lookup('helper:' + helperName)|| {}).helperFunction || (container.lookup('component:' + helperName) || {}).helperFunction; | |
var helper = atResolvedHelper || atHelpers || atContainer; | |
if(!helper) { | |
var normalisedHelperName = "helper:" + helperName; | |
var componentLookup = container.lookup("component-lookup:main"); | |
var Component = componentLookup.lookupFactory(helperName, container); | |
if (Component) { | |
helper = Ember.HTMLBars.makeViewHelper(Component); | |
container._registry.register(normalisedHelperName, helper); | |
return helper.helperFunction; | |
} | |
Ember.Logger.error('Can\'t find helper for ' + helperName); | |
} else { | |
return helper; | |
} | |
}, | |
component = resolvePath(componentPath) || componentPath, | |
helper = getHelper(options.data.view.container, component), | |
hash = options.hash; | |
// Allow to pass params as hash-object | |
if ('___params' in hash) { | |
var params = resolvePath(hash.___params); | |
delete hash.___params; | |
hash = merge(params, hash); | |
options.hash = hash; | |
} | |
// Allow to specify which params should be resolved | |
each(hash, function(key, value) { | |
var newKey = key.replace('__', ''); | |
if ((key.indexOf('__') === 0)) { | |
options.hash[newKey] = value;//resolvePath(value); | |
options.hashContexts[newKey] = get(view, 'controller'); | |
options.hashTypes[newKey] = "ID"; | |
} | |
}); | |
// For cases when we need pass into component not only attributes but param too | |
// When we need this? | |
// for example you want to render | |
// {{componentName paramName someOption=someOptionValue}} | |
// You can do | |
// {{#render-component 'componentName' someOption=someOptionValue}} | |
// BUT! You can't do ( you can't pass more than one argument into component ) | |
// {{#render-component 'componentName' paramName someOption=someOptionValue}} | |
// so for such cases you can use | |
// {{#render-component 'componentName' _param=paramName someOption=someOptionValue}} | |
// or | |
// {{#render-component 'componentName' __params=paramsHash}} | |
// where paramsHash is | |
// paramsHash = { _param:paramName, someOption=someOptionValue } | |
var param; | |
if ('_param' in hash) { | |
param = resolvePath(hash._param); | |
delete hash._param; | |
} | |
if(!helper) { | |
Ember.Logger.warn('render-component can\'t find handler for "' + component + '"'); | |
var hooks = options.hooks; | |
hooks.block(options, options.morph, view, component, param? [param]: [], hash); | |
} else if(param) { | |
//helper.call(this, param, options, options, options); // this strange params for instanceHelper: function(newView, hash, options, env) { at ember.debug.js | |
helper.call(this, param, options); | |
} else { | |
//helper.call(this, [], options, options, options, options); | |
helper.call(this, [], hash, options, options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment