Comparing version 0.7.0 to 1.0.0
463
MVCObject.js
@@ -1,18 +0,24 @@ | ||
(function(){ | ||
function Accessor(target, targetKey){ | ||
var self = this; | ||
self.target = target; | ||
self.targetKey = targetKey; | ||
} | ||
Accessor.prototype.transform = function(from, to){ | ||
var self = this; | ||
self.from = from; | ||
self.to = to; | ||
self.target.notify(self.targetKey); | ||
return self; | ||
} | ||
var MVCObject = (function(){ | ||
(function (factory) { | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
var v = factory(require, exports); if (v !== undefined) module.exports = v; | ||
} | ||
else if (typeof define === 'function' && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
var Accessor = (function () { | ||
function Accessor(target, targetKey) { | ||
this.target = target; | ||
this.targetKey = targetKey; | ||
} | ||
; | ||
Accessor.prototype.transform = function (from, to) { | ||
this.from = from; | ||
this.to = to; | ||
this.target.notify(this.targetKey); | ||
}; | ||
return Accessor; | ||
}()); | ||
exports.Accessor = Accessor; | ||
var getterNameCache = {}; | ||
@@ -24,31 +30,27 @@ var setterNameCache = {}; | ||
var uid = '__uid__'; | ||
function capitalize(str){ | ||
return str.substr(0, 1).toUpperCase() + str.substr(1); | ||
function capitalize(str) { | ||
return str.substr(0, 1).toUpperCase() + str.substr(1); | ||
} | ||
function getUid(obj){ | ||
return obj[uid] || (obj[uid] = ++uuid); | ||
function getUid(obj) { | ||
return obj[uid] || (obj[uid] = ++uuid); | ||
} | ||
function toKey(key){ | ||
return '_' + key; | ||
function toKey(key) { | ||
return '_' + key; | ||
} | ||
function getGetterName(key){ | ||
if(getterNameCache.hasOwnProperty(key)){ | ||
return getterNameCache[key]; | ||
}else{ | ||
return getterNameCache[key] = 'get' + capitalize(key); | ||
} | ||
function getGetterName(key) { | ||
if (getterNameCache.hasOwnProperty(key)) { | ||
return getterNameCache[key]; | ||
} | ||
else { | ||
return getterNameCache[key] = 'get' + capitalize(key); | ||
} | ||
} | ||
function getSetterName(key){ | ||
if(setterNameCache.hasOwnProperty(key)){ | ||
return setterNameCache[key]; | ||
}else{ | ||
return setterNameCache[key] = 'set' + capitalize(key); | ||
} | ||
function getSetterName(key) { | ||
if (setterNameCache.hasOwnProperty(key)) { | ||
return setterNameCache[key]; | ||
} | ||
else { | ||
return setterNameCache[key] = 'set' + capitalize(key); | ||
} | ||
} | ||
/** | ||
@@ -64,203 +66,188 @@ * @description 这个函数的触发需要时机 | ||
*/ | ||
function triggerChange(target, targetKey){ | ||
var evt = targetKey + '_changed'; | ||
/** | ||
* 优先检测并执行目标对象key对应的响应方法 | ||
* 其次检测并执行默认方法 | ||
*/ | ||
if(target[evt]){ | ||
target[evt](); | ||
}else if(typeof target.changed === 'function'){ | ||
target.changed(targetKey); | ||
} | ||
if(target[bindings] && target[bindings][targetKey]){ | ||
var ref = target[bindings][targetKey]; | ||
var bindingObj, bindingUid; | ||
for(bindingUid in ref){ | ||
if(ref.hasOwnProperty(bindingUid)){ | ||
bindingObj = ref[bindingUid]; | ||
triggerChange(bindingObj.target, bindingObj.targetKey); | ||
} | ||
function triggerChange(target, targetKey) { | ||
var evt = targetKey + '_changed'; | ||
/** | ||
* 优先检测并执行目标对象key对应的响应方法 | ||
* 其次检测并执行默认方法 | ||
*/ | ||
if (target[evt]) { | ||
target[evt](); | ||
} | ||
} | ||
} | ||
function MVCObject(){}; | ||
var proto = MVCObject.prototype; | ||
/** | ||
* @description 从依赖链中获取对应key的值 | ||
* @param {String} key 关键值 | ||
* @return {mixed} 对应的值 | ||
*/ | ||
proto.get = function(key){ | ||
var self = this; | ||
if(self[accessors] && self[accessors].hasOwnProperty(key)){ | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
var getterName = getGetterName(targetKey); | ||
var value; | ||
if(target[getterName]){ | ||
value = target[getterName](); | ||
}else{ | ||
value = target.get(targetKey); | ||
else if (typeof target.changed === 'function') { | ||
target.changed(targetKey); | ||
} | ||
if(accessor.to){ | ||
value = accessor.to(value); | ||
if (target[bindings] && target[bindings][targetKey]) { | ||
var ref = target[bindings][targetKey]; | ||
var bindingObj, bindingUid; | ||
for (bindingUid in ref) { | ||
if (ref.hasOwnProperty(bindingUid)) { | ||
bindingObj = ref[bindingUid]; | ||
triggerChange(bindingObj.target, bindingObj.targetKey); | ||
} | ||
} | ||
} | ||
}else if(self.hasOwnProperty(toKey(key))){ | ||
value = self[toKey(key)]; | ||
} | ||
return value; | ||
}; | ||
/** | ||
* @description set方法遍历依赖链直到找到key的持有对象设置key的值; | ||
* 有三个分支 | ||
* @param {String} key 关键值 | ||
* @param {all} value 要给key设定的值,可以是所有类型 | ||
* @return {this} | ||
*/ | ||
proto.set = function(key, value){ | ||
var self = this; | ||
if(self[accessors] && self[accessors].hasOwnProperty(key)){ | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
var setterName = getSetterName(targetKey); | ||
if(accessor.from){ | ||
value = accessor.from(value); | ||
} | ||
var MVCObject = (function () { | ||
function MVCObject() { | ||
} | ||
if(target[setterName]){ | ||
target[setterName](value); | ||
}else{ | ||
target.set(targetKey, value); | ||
} | ||
}else{ | ||
this[toKey(key)] = value; | ||
triggerChange(self, key); | ||
} | ||
return self; | ||
}; | ||
/** | ||
* @description 没个MVCObject对象各自的响应对应的key值变化时的逻辑 | ||
*/ | ||
proto.changed = function(){}; | ||
/** | ||
* @description 手动触发对应key的事件传播 | ||
* @param {String} key 关键值 | ||
* @return {this} | ||
*/ | ||
proto.notify = function(key){ | ||
var self = this; | ||
if(self[accessors] && self[accessors].hasOwnProperty(key)){ | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
target.notify(targetKey); | ||
}else{ | ||
triggerChange(self, key); | ||
} | ||
return self; | ||
}; | ||
proto.setValues = function(values){ | ||
var self = this; | ||
var key, setterName, value; | ||
for(key in values){ | ||
if(values.hasOwnProperty(key)){ | ||
value = values[key]; | ||
setterName = getSetterName(key); | ||
if(self[setterName]){ | ||
self[setterName](value); | ||
}else{ | ||
self.set(key, value); | ||
} | ||
} | ||
} | ||
return self; | ||
}; | ||
/** | ||
* @description 将当前对象的一个key与目标对象的targetKey建立监听和广播关系 | ||
* @param key {String} 当前对象上的key | ||
* @param target {Object} 目标对象 | ||
* @param tarrgetKey {String} 目标对象上的key | ||
* @param noNotify {Boolean} | ||
* @return {Accessor} | ||
*/ | ||
proto.bindTo = function(key, target, targetKey, noNotify){ | ||
targetKey || (targetKey = key); | ||
var self = this; | ||
self.unbind(key); | ||
self[accessors] || (self[accessors] = {}); | ||
target[bindings] || (target[bindings] = {}); | ||
target[bindings][targetKey] || (target[bindings][targetKey] = {}); | ||
var binding = new Accessor(self, key); | ||
var accessor = new Accessor(target, targetKey); | ||
self[accessors][key] = accessor; | ||
target[bindings][targetKey][getUid(self)] = binding; | ||
if(!noNotify){ | ||
triggerChange(self, key); | ||
} | ||
return accessor; | ||
}; | ||
/** | ||
* @description 解除当前对象上key与目标对象的监听 | ||
* @param {String} key 关键字 | ||
* @return {this} | ||
*/ | ||
proto.unbind = function(key){ | ||
var self = this; | ||
if(self[accessors]){ | ||
var accessor = self[accessors][key]; | ||
if(accessor){ | ||
var target = accessor.target; | ||
var targetKey = accessor.targetKey; | ||
self[toKey(key)] = self.get(key); | ||
delete target[bindings][targetKey][getUid(self)]; | ||
delete self[accessors][key]; | ||
} | ||
} | ||
return self; | ||
} | ||
proto.unbindAll = function(){ | ||
var self = this; | ||
if(self[accessors]){ | ||
var ref = self[accessors]; | ||
for(var key in ref){ | ||
if(ref.hasOwnProperty(key)){ | ||
/** | ||
* @description 从依赖链中获取对应key的值 | ||
* @param {String} key 关键值 | ||
* @return {mixed} 对应的值 | ||
*/ | ||
MVCObject.prototype.get = function (key) { | ||
var self = this; | ||
if (self[accessors] && self[accessors].hasOwnProperty(key)) { | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
var getterName = getGetterName(targetKey); | ||
var value; | ||
if (target[getterName]) { | ||
value = target[getterName](); | ||
} | ||
else { | ||
value = target.get(targetKey); | ||
} | ||
if (accessor.to) { | ||
value = accessor.to(value); | ||
} | ||
} | ||
else if (self.hasOwnProperty(toKey(key))) { | ||
value = self[toKey(key)]; | ||
} | ||
return value; | ||
}; | ||
/** | ||
* @description set方法遍历依赖链直到找到key的持有对象设置key的值; | ||
* 有三个分支 | ||
* @param {String} key 关键值 | ||
* @param {all} value 要给key设定的值,可以是所有类型 | ||
* @return {this} | ||
*/ | ||
MVCObject.prototype.set = function (key, value) { | ||
var self = this; | ||
if (self[accessors] && self[accessors].hasOwnProperty(key)) { | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
var setterName = getSetterName(targetKey); | ||
if (accessor.from) { | ||
value = accessor.from(value); | ||
} | ||
if (target[setterName]) { | ||
target[setterName](value); | ||
} | ||
else { | ||
target.set(targetKey, value); | ||
} | ||
} | ||
else { | ||
this[toKey(key)] = value; | ||
triggerChange(self, key); | ||
} | ||
return self; | ||
}; | ||
/** | ||
* @description 没个MVCObject对象各自的响应对应的key值变化时的逻辑 | ||
*/ | ||
MVCObject.prototype.changed = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i - 0] = arguments[_i]; | ||
} | ||
}; | ||
/** | ||
* @description 手动触发对应key的事件传播 | ||
* @param {String} key 关键值 | ||
* @return {this} | ||
*/ | ||
MVCObject.prototype.notify = function (key) { | ||
var self = this; | ||
if (self[accessors] && self[accessors].hasOwnProperty(key)) { | ||
var accessor = self[accessors][key]; | ||
var targetKey = accessor.targetKey; | ||
var target = accessor.target; | ||
target.notify(targetKey); | ||
} | ||
else { | ||
triggerChange(self, key); | ||
} | ||
return self; | ||
}; | ||
MVCObject.prototype.setValues = function (values) { | ||
var self = this; | ||
var key, setterName, value; | ||
for (key in values) { | ||
if (values.hasOwnProperty(key)) { | ||
value = values[key]; | ||
setterName = getSetterName(key); | ||
if (self[setterName]) { | ||
self[setterName](value); | ||
} | ||
else { | ||
self.set(key, value); | ||
} | ||
} | ||
} | ||
return self; | ||
}; | ||
/** | ||
* @description 将当前对象的一个key与目标对象的targetKey建立监听和广播关系 | ||
* @param key {String} 当前对象上的key | ||
* @param target {Object} 目标对象 | ||
* @param tarrgetKey {String} 目标对象上的key | ||
* @param noNotify {Boolean} | ||
* @return {Accessor} | ||
*/ | ||
MVCObject.prototype.bindTo = function (key, target, targetKey, noNotify) { | ||
if (targetKey === void 0) { targetKey = key; } | ||
var self = this; | ||
self.unbind(key); | ||
} | ||
} | ||
} | ||
return self; | ||
}; | ||
return MVCObject; | ||
})(); | ||
if (typeof module !== "undefined" && module !== null) { | ||
module.exports = MVCObject; | ||
} else if (typeof define === 'function') { | ||
define(function() { | ||
return MVCObject; | ||
}); | ||
} else { | ||
window.MVCObject = MVCObject; | ||
} | ||
})(); | ||
self[accessors] || (self[accessors] = {}); | ||
target[bindings] || (target[bindings] = {}); | ||
target[bindings][targetKey] || (target[bindings][targetKey] = {}); | ||
var binding = new Accessor(self, key); | ||
var accessor = new Accessor(target, targetKey); | ||
self[accessors][key] = accessor; | ||
target[bindings][targetKey][getUid(self)] = binding; | ||
if (!noNotify) { | ||
triggerChange(self, key); | ||
} | ||
return accessor; | ||
}; | ||
/** | ||
* @description 解除当前对象上key与目标对象的监听 | ||
* @param {String} key 关键字 | ||
* @return {this} | ||
*/ | ||
MVCObject.prototype.unbind = function (key) { | ||
var self = this; | ||
if (self[accessors]) { | ||
var accessor = self[accessors][key]; | ||
if (accessor) { | ||
var target = accessor.target; | ||
var targetKey = accessor.targetKey; | ||
self[toKey(key)] = self.get(key); | ||
delete target[bindings][targetKey][getUid(self)]; | ||
delete self[accessors][key]; | ||
} | ||
} | ||
return self; | ||
}; | ||
MVCObject.prototype.unbindAll = function () { | ||
var self = this; | ||
if (self[accessors]) { | ||
var ref = self[accessors]; | ||
for (var key in ref) { | ||
if (ref.hasOwnProperty(key)) { | ||
self.unbind(key); | ||
} | ||
} | ||
} | ||
return self; | ||
}; | ||
return MVCObject; | ||
}()); | ||
exports.MVCObject = MVCObject; | ||
}); |
{ | ||
"name": "mvcobject", | ||
"version": "0.7.0", | ||
"version": "1.0.0", | ||
"description": "An implementation of Google Maps' MVCObject for Node.js and browers", | ||
@@ -10,4 +10,6 @@ "main": "MVCObject.js", | ||
"scripts": { | ||
"test": "./node_modules/.bin/mocha" | ||
"test": "mocha", | ||
"build": "tsc -d -m umd MVCObject.ts && uglifyjs -m -c -o MVCObject.min.js -- MVCObject.js" | ||
}, | ||
"types": "MVCObject.d.ts", | ||
"repository": { | ||
@@ -35,4 +37,5 @@ "type": "git", | ||
"sinon-chai": "~2.7.0", | ||
"typescript": "^2.0.3", | ||
"uglify-js": "^2.4.19" | ||
} | ||
} |
@@ -24,33 +24,19 @@ ## MVCObject [![NPM version](https://badge.fury.io/js/mvcobject.png)](http://badge.fury.io/js/mvcobject) [![Build Status](https://travis-ci.org/Treri/MVCObject.png)](https://travis-ci.org/Treri/MVCObject) [![Dependencies Status](https://david-dm.org/Treri/MVCObject.png)](https://david-dm.org/Treri/MVCObject) | ||
0. Install | ||
- npm | ||
npm install mvcobject | ||
```bash | ||
npm install mvcobject | ||
``` | ||
- bower | ||
1. ES6 | ||
bower install mvcobject | ||
```js | ||
import {MVCObject} from 'MVCObject' | ||
1. CoffeeScript in Node.js | ||
class Foo extends MVCObject{ | ||
constructor(){ | ||
super() | ||
} | ||
} | ||
``` | ||
MVCObject = require 'mvcobject' | ||
class class1 extends MVCObject | ||
constructor: -> | ||
... | ||
2. JavaScript in Node.js | ||
var MVCObject = require('mvcobject'); | ||
function class1(){} | ||
class1.prototype = new MVCObject(); | ||
3. JavaScript in Browsers | ||
<!-- include the MVCObject.js --> | ||
<script src="/path/to/MVCObject.js"></script> | ||
function class1(){} | ||
class1.prototype = new MVCObject(); | ||
### test | ||
@@ -60,3 +46,3 @@ Thanks to @twpayne, the test suites are based on his [mvcobject_test.js](https://github.com/twpayne/mvcobject/blob/master/src/mvc/mvcobject_test.js). | ||
npm install | ||
make test | ||
npm run test | ||
@@ -66,3 +52,3 @@ ### LICENSE | ||
Copyright (c) 2013-2015 Treri treri.liu@gmail.com | ||
Copyright (c) 2013-2016 Treri treri.liu@gmail.com | ||
@@ -69,0 +55,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
@@ -5,5 +5,5 @@ "use strict"; | ||
global.should = chai.should(); | ||
global.MVCObject = require('../MVCObject'); | ||
global.MVCObject = require('../MVCObject').MVCObject; | ||
var sinonChai = require('sinon-chai'); | ||
chai.use(sinonChai); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
38105
525
1
7
68