-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support init
or setup
returning something other than new instance
#30
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -284,6 +284,9 @@ assign(Construct, { | |
writable: true | ||
}); | ||
args = inst.setup.apply(inst, arguments); | ||
if (args instanceof Construct.ReturnValue){ | ||
return args.value; | ||
} | ||
inst.__inSetup = false; | ||
} | ||
// Call `init` if there is an `init` | ||
|
@@ -526,6 +529,49 @@ assign(Construct, { | |
* dog.speak(); // 'woof' | ||
* blackMamba.speak(); // 'ssssss' | ||
* ``` | ||
* | ||
* ## Alternative value for a new instance | ||
* | ||
* Sometimes you may want to return some custom value instead of a new object when creating an instance of your class. | ||
* For example, you want your class to act as a singleton, or check whether an item with the given id was already | ||
* created and return an existing one from your cache store (e.g. using [can-connect/constructor/store/store]). | ||
* | ||
* To achieve this you can return [can-construct.ReturnValue] from `setup` method of your class. | ||
* | ||
* Lets say you have `myStore` to cache all newly created instances. And if an item already exists you want to merge | ||
* the new data into the existing instance and return the updated instance. | ||
* | ||
* ``` | ||
* const myStore = {}; | ||
* | ||
* const Item = Construct.extend({ | ||
* setup: function(params){ | ||
* if (myStore[params.id]){ | ||
* var item = myStore[params.id]; | ||
* | ||
* // Merge new data to the existing instance: | ||
* Object.keys( params ).forEach(function( key ){ | ||
* item[key] = params[key]; | ||
* }); | ||
* | ||
* // Return the updated item: | ||
* return new Construct.ReturnValue( item ); | ||
* } else { | ||
* return [params]; | ||
* } | ||
* }, | ||
* init: function(params){ | ||
* this.id = params.id; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
* this.name = params.name; | ||
* | ||
* // Save to cache store: | ||
* myStore[this.id] = this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should almost certainly be done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
* } | ||
* }); | ||
* | ||
* const item_1 = new Item( {id: 1, name: "One"} ); | ||
* const item_1a = new Item( {id: 1, name: "OnePlus"} ) | ||
* ``` | ||
*/ | ||
extend: function (name, staticProperties, instanceProperties) { | ||
var shortName = name, | ||
|
@@ -663,6 +709,40 @@ assign(Construct, { | |
* console.log(Counter.count); // 1 | ||
* ``` | ||
*/ | ||
}, | ||
/** | ||
* @function can-construct.ReturnValue ReturnValue | ||
* @parent can-construct.static | ||
* | ||
* A constructor function which `value` property will be used as a value for a new instance. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too much detail for a description. It shouldn't care at all about the
|
||
* | ||
* @signature `new Construct.ReturnValue( value )` | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signature should talk about how this works. Also, the example code below should be put in the signature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
* @param {*} value A value to be used for a new instance instead of a new object. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, indent 2 spaces There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
* | ||
* ``` | ||
* const Student = function( name, school ){ | ||
* this.name = name; | ||
* this.school = school; | ||
* } | ||
* | ||
* const Person = Construct.extend({ | ||
* setup: function( options ){ | ||
* if (options.school){ | ||
* return new Constructor.ReturnValue( new Student( options.name, options.school ) ); | ||
* } else { | ||
* return [options]; | ||
* } | ||
* } | ||
* }); | ||
* | ||
* const myPerson = new Person( {name: "Peter", school: "UFT"} ); | ||
* | ||
* myPerson instanceof Student // => true | ||
* ``` | ||
*/ | ||
ReturnValue: function(value){ | ||
this.value = value; | ||
} | ||
}); | ||
/** | ||
|
@@ -675,8 +755,9 @@ assign(Construct, { | |
* | ||
* @param {*} args The arguments passed to the constructor. | ||
* | ||
* @return {Array|undefined} If an array is returned, the array's items are passed as | ||
* arguments to [can-construct::init init]. The following example always makes | ||
* @return {Array|undefined|can-construct.ReturnValue} If an array is returned, the array's items are passed as | ||
* arguments to [can-construct::init init]. If [can-construct.ReturnValue] is returned then the value that passed to it | ||
* will be used as a value for a new instance. The following example always makes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change:
To something more like: If a [can-construct.ReturnValue] instance is returned, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
* sure that init is called with a jQuery wrapped element: | ||
* | ||
* ```js | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably use
Object.assign
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.