Skip to content

Commit

Permalink
improved docs for can.Component
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Sep 12, 2013
1 parent d98c91d commit 26d6cbf
Show file tree
Hide file tree
Showing 21 changed files with 1,219 additions and 124 deletions.
29 changes: 25 additions & 4 deletions component/component.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
steal("can/util","can/control","can/observe","can/view/mustache","can/view/mustache/bindings",function(can){

var Component = can.Component = can.Construct.extend({
var ignoreAttributesRegExp = /data-view-id|class|id/i
/**
* @add can.Component
*/
var Component = can.Component = can.Construct.extend(
/**
* @static
*/
{
setup: function(){
can.Construct.setup.apply( this, arguments );

if(can.Component){
var self = this;
this.Control = can.Control.extend({
_lookup: function(options){
return [options.scope, window]
return [options.scope, options, window]
}
},can.extend({
setup: function(el, options){
Expand Down Expand Up @@ -61,6 +69,9 @@ steal("can/util","can/control","can/observe","can/view/mustache","can/view/musta

}
},{
/**
* @prototype
*/
setup: function(el, hookupOptions){
// Setup values passed to component
var initalScopeData = {},
Expand All @@ -72,13 +83,14 @@ steal("can/util","can/control","can/observe","can/view/mustache","can/view/musta
})

// get the value in the scope for each attribute
// the hookup should probably happen after?
can.each(can.makeArray(el.attributes), function(node, index){

var name = node.nodeName.toLowerCase(),
value = node.value;

// ignore attributes already in ScopeMappings
if(component.constructor.attributeScopeMappings[name] || name === "data-view-id"){
if(component.constructor.attributeScopeMappings[name] || ignoreAttributesRegExp.test(name)){
return;
}

Expand Down Expand Up @@ -112,7 +124,16 @@ steal("can/util","can/control","can/observe","can/view/mustache","can/view/musta
if(this.constructor.Map){
componentScope = new this.constructor.Map(initalScopeData);
} else if(can.isFunction(this.scope)){
componentScope = new ( can.Map.extend(this.scope(el)) )(initalScopeData);
var scopeResult = this.scope(initalScopeData, hookupOptions.scope, el);
// if the function returns a can.Map, use that as the scope
if(scopeResult instanceof can.Map){
componentScope = scopeResult
} else if(typeof scopeResult == "function" && typeof scopeResult.extend == "function"){
componentScope = new scopeResult(initalScopeData);
} else {
componentScope = new ( can.Map.extend(scopeResult) )(initalScopeData);
}

}

this.scope = componentScope;
Expand Down
Loading

0 comments on commit 26d6cbf

Please sign in to comment.