Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added a 'keys' event if a parent object recieves a new key

  • Loading branch information...
commit 5b749574bb1f99dabc249dad607eca7ccacbda6c 1 parent 904349e
@KoryNunn KoryNunn authored
View
8 events.js
@@ -37,7 +37,7 @@ module.exports = function(modelGet, gel, PathToken){
ModelEventEmitter.prototype.pushPath = function(path, type, skipReferences){
var currentEvent = this.events[path];
- if(!currentEvent || type === 'target'){
+ if(!currentEvent || type === 'target' || type === 'keys'){
this.events[path] = type;
}
@@ -128,7 +128,7 @@ module.exports = function(modelGet, gel, PathToken){
}
}
- function trigger(path){
+ function trigger(path, keysChange){
// resolve path to root
path = paths.resolve(paths.createRoot(), path);
@@ -136,6 +136,10 @@ module.exports = function(modelGet, gel, PathToken){
bubbleTrigger(path, emitter);
+ if(keysChange){
+ emitter.pushPath(paths.resolve(path ,pathConstants.upALevel), 'keys');
+ }
+
emitter.pushPath(path, 'target');
sinkTrigger(path, emitter);
View
4 gedi.js
@@ -277,11 +277,11 @@ function newGedi(model) {
var previousValue = get(expression, model);
- set(expression, value, model);
+ var keysChanged = set(expression, value, model);
if(!(value instanceof DeletedItem)){
events.addModelReference(expression, value);
- events.trigger(expression);
+ events.trigger(expression, keysChanged);
}
if(!(value && typeof value !== 'object') && previousValue && typeof previousValue === 'object'){
View
8 modelOperations.js
@@ -91,7 +91,8 @@ function set(path, value, model) {
index = 1;
}
- var reference = model;
+ var reference = model,
+ keysChanged;
for(; index < pathLength; index++){
var key = pathParts[index];
@@ -108,6 +109,9 @@ function set(path, value, model) {
}
if (index === pathLength - 1) {
// if we are at the end of the line, set to the model
+ if(!(key in reference)){
+ keysChanged = true;
+ }
reference[key] = value;
}
//otherwise, dig deeper
@@ -115,6 +119,8 @@ function set(path, value, model) {
reference = reference[key];
}
}
+
+ return keysChanged;
}
module.exports = {
View
3  package.json
@@ -12,7 +12,8 @@
"weakmap": "0.0.x"
},
"devDependencies": {
- "tape": "x.x.x"
+ "tape": "x.x.x",
+ "grape": "~0.1.8"
},
"testling": {
"files": "test/all.js",
View
64 set.js
@@ -1,64 +0,0 @@
-
-function set(path, value, model) {
- // passed a null or undefined path, do nothing.
- if (!path) {
- return;
- }
-
- memoiseCache = {};
-
- // If you just pass in an object, you are overwriting the model.
- if (typeof path === "object") {
- value = path;
- path = paths.createRoot();
- }
-
- var pathParts = paths.toParts(path),
- index = 0,
- pathLength = pathParts.length;
-
- if(paths.isRoot(path)){
- overwriteModel(value, model);
- return;
- }
-
- if(paths.isAbsolute(path)){
- index = 1;
- }
-
- var reference = model;
-
- for(; index < pathLength; index++){
- var key = pathParts[index];
-
- // if we have hit a non-object property on the reference and we have more keys after this one
- // make an object (or array) here and move on.
- if ((typeof reference[key] !== "object" || reference[key] === null) && index < pathLength - 1) {
- if (!isNaN(key)) {
- reference[key] = [];
- }
- else {
- reference[key] = {};
- }
- }
- if (index === pathLength - 1) {
- // if we are at the end of the line, set to the model
- reference[key] = value;
- }
- //otherwise, RECURSANIZE!
- else {
- reference = reference[key];
- }
- }
-}
-
-function overwriteModel(replacement, model){
- for (var modelProp in model) {
- delete model[modelProp];
- }
- for (var replacementProp in replacement) {
- model[replacementProp] = replacement[replacementProp];
- }
-}
-
-module.exports = set;
View
18 test/bind.js
@@ -21,7 +21,7 @@ test('array bind', function(t) {
t.plan(1);
gedi.set('[things]', []);
- gedi.bind('[things]', function(){
+ gedi.bind('[things]', function(event){
t.pass('detected [things] change');
});
@@ -29,6 +29,22 @@ test('array bind', function(t) {
});
+test('array length bind', function(t) {
+ var gedi = new Gedi();
+
+ t.plan(1);
+
+ gedi.set('[things]', []);
+ gedi.bind('[things].length', function(event){
+ if(event.captureType === 'keys'){
+ t.pass('detected [things/length] change');
+ }
+ });
+
+ gedi.set('[things/0]', 'stuff');
+
+});
+
test('nested bind', function(t) {
var gedi = new Gedi();
Please sign in to comment.
Something went wrong with that request. Please try again.