diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bc2ba5d --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/can-define.js b/can-define.js new file mode 100644 index 0000000..7ab1ab8 --- /dev/null +++ b/can-define.js @@ -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; +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..a9f4500 --- /dev/null +++ b/package.json @@ -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" + } +} \ No newline at end of file diff --git a/test.html b/test.html new file mode 100644 index 0000000..29a7969 --- /dev/null +++ b/test.html @@ -0,0 +1 @@ + diff --git a/test.js b/test.js new file mode 100644 index 0000000..322063d --- /dev/null +++ b/test.js @@ -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(); + +}); + +