Skip to content

Commit

Permalink
a basic working version of can-derive
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Dec 28, 2015
1 parent 08cc9fc commit 783c140
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 CanJS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

66 changes: 66 additions & 0 deletions can-define.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

var can = require("can/util/");
var event = require("can/event/");
var compute = require("can/compute/");
var Map = require("can/map/");
var mapHelpers = require("can/map/map_helpers");



module.exports = function(objPrototype, defines){

// Copy every method on Map
can.each(Map.prototype, function(value, prop){
objPrototype[prop]= value;
});

objPrototype.___set = function (prop, val) {
var computedAttr = this._computedAttrs[prop];
if ( computedAttr ) {
computedAttr.compute(val);
} else {
this._data[prop] = val;
}
};

Object.defineProperty(objPrototype, "_computedAttrs", {
get: function(){
if(!this.__computeAttrs) {
this.__computeAttrs = {};
for (var attr in defines) {
var def = defines[attr],
get = def.get;
if (get) {
mapHelpers.addComputedAttr(this, attr, can.compute.async(undefined, get, this));
}
}

}
return this.__computeAttrs;
}
});
Object.defineProperty(objPrototype, "_data", {
get: function(){
if(!this.__data) {
this.__data = {};
}
return this.__data;
}
});


can.each(defines, function(value, prop){
Object.defineProperty(objPrototype, prop,{
get: function(){
return this._get(prop);
},
set: function(val){
return this._set(prop, val);
}
});
});



return objPrototype;
};
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "can-define",
"version": "0.0.1",
"description": "Like can.Map, but without the .attr method.",
"main": "can-define.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/canjs/can-define"
},
"keywords": [
"CanJS"
],
"author": "Bitovi",
"license": "MIT",
"bugs": {
"url": "https://github.com/canjs/can-define/issues"
},
"homepage": "https://github.com/canjs/can-define",
"dependencies": {
"can": "^2.3.0",
"jquery": "^2.1.4",
"steal": "^0.12.0",
"steal-qunit": "^0.1.1"
}
}
1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script src="./node_modules/steal/steal.js" main="can-define/test"></script>
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var QUnit = require("steal-qunit");
var compute = require("can/compute/");
var define = require("can-define");
var stache = require("can/view/stache/");


QUnit.test("basics on a prototype", function(){

var Person = function(first, last){
this.first = first;
this.last = last;
};
define(Person.prototype,{
first: {
type: "string"
},
last: {
type: "string"
},
fullName: {
get: function(){
return this.first+" "+this.last;
}
}
});

var p = new Person("Mohamed", "Cherif");

p.bind("fullName", function(ev, newVal, oldVal){
QUnit.equal(oldVal, "Mohamed Cherif");
QUnit.equal(newVal, "Justin Meyer");
});

p.bind("first", function(el, newVal, oldVal){
QUnit.equal(newVal, "Justin", "first new value");
QUnit.equal(oldVal, "Mohamed", "first old value");
});

can.batch.start();
p.first = "Justin";
p.last = "Meyer";
can.batch.stop();

});


0 comments on commit 783c140

Please sign in to comment.