Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix unset for dot access

  • Loading branch information...
commit 6133cb487a782dbfb9c065cd6e8508d2ac7dcbe2 1 parent f7166a0
@rhysbrettbowen authored
Showing with 120 additions and 90 deletions.
  1. +43 −43 collection.js
  2. +10 −7 control.js
  3. +5 −0 mod.js
  4. +60 −38 model.js
  5. +2 −2 router.js
View
86 collection.js
@@ -1,22 +1,22 @@
-/*********************************************************************************
-**********************************************************************************
-** **
-** Copyright (c) 2012 Catch.com, Inc. **
-** **
-** Licensed under the Apache License, Version 2.0 (the "License"); **
-** you may not use this file except in compliance with the License. **
-** You may obtain a copy of the License at **
-** **
-** http://www.apache.org/licenses/LICENSE-2.0 **
-** **
-** Unless required by applicable law or agreed to in writing, software **
-** distributed under the License is distributed on an "AS IS" BASIS, **
-** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. **
-** See the License for the specific language governing permissions and **
-** limitations under the License. **
-** **
-**********************************************************************************
-*********************************************************************************/
+/*******************************************************************************
+********************************************************************************
+** **
+** Copyright (c) 2012 Catch.com, Inc. **
+** **
+** Licensed under the Apache License, Version 2.0 (the "License"); **
+** you may not use this file except in compliance with the License. **
+** You may obtain a copy of the License at **
+** **
+** http://www.apache.org/licenses/LICENSE-2.0 **
+** **
+** Unless required by applicable law or agreed to in writing, software **
+** distributed under the License is distributed on an "AS IS" BASIS, **
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. **
+** See the License for the specific language governing permissions and **
+** limitations under the License. **
+** **
+********************************************************************************
+*******************************************************************************/
goog.provide('mvc.Collection');
@@ -49,52 +49,52 @@ mvc.Collection = function(opt_options) {
goog.object.remove(opt_options, 'models');
/**
- * @private
- * @type {Array.<{model:mvc.Model}>}
- */
+ * @private
+ * @type {Array.<{model:mvc.Model}>}
+ */
this.models_ = [];
this.comparator_ = [defaults['comparator'] &&
goog.bind(defaults['comparator'], this)];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.modelChangeFns_ = [];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.anyModelChangeFns_ = [];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.addedModels_ = [];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.addedModelsFns_ = [];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.removedModels_ = [];
/**
- * @private
- * @type {Array}
- */
+ * @private
+ * @type {Array}
+ */
this.removedModelsFns_ = [];
@@ -103,9 +103,9 @@ mvc.Collection = function(opt_options) {
this.anyModelChange_ = [false];
/**
- * @private
- * @type {function(new:mvc.Model, Object=)}
- */
+ * @private
+ * @type {function(new:mvc.Model, Object=)}
+ */
this.modelType_ = defaults['modelType'];
goog.base(this, opt_options);
View
17 control.js
@@ -224,17 +224,20 @@ mvc.Control.prototype.autobindChange_ = function(handle, selector) {
}
}, this);
}
- goog.array.forEach(this.getEls(selector), function(el) {
- if (el.tagName == 'INPUT' && el.getAttribute('type') == 'checkbox') {
- el.checked = this.getModel().get(handle.reqs[0]);
- }
- }, this);
+ if (!handle.noCheck)
+ goog.array.forEach(this.getEls(selector), function(el) {
+ if (el.tagName == 'INPUT' && el.getAttribute('type') == 'checkbox') {
+ el.checked = !!this.getModel().get(handle.reqs[0]);
+ }
+ }, this);
if (goog.isDef(handle.onClass)) {
var onClass = handle.onClass;
goog.array.forEach(this.getEls(selector), function(el) {
- goog.dom.classes.enable(el, onClass, first);
+ goog.dom.classes.enable(el, onClass,
+ first && first.length !== 0);
if(handle.offClass)
- goog.dom.classes.enable(el, handle.offClass, !first);
+ goog.dom.classes.enable(el, handle.offClass,
+ !first || first.length === 0);
});
}
if (handle.reqClass) {
View
5 mod.js
@@ -110,6 +110,11 @@ mvc.Filter = {
/** @this {mvc.Mod} */
modelChange: function(fn, opt_handler) {
return mvc.Collection.prototype.modelChange.apply(this, arguments);
+ },
+
+ /** @this {mvc.Mod} */
+ getLength: function() {
+ return this.getModels().length;
}
};
View
98 model.js
@@ -62,21 +62,21 @@ mvc.Model = function(opt_options) {
/**
- * @private
- * @type {Object.<string, ?Object>}
- */
+ * @private
+ * @type {Object.<string, ?Object>}
+ */
this.attr_ = {};
/**
- * @private
- * @type {Object.<{attr: Array.<string>, fn: Function}>}
- */
+ * @private
+ * @type {Object.<{attr: Array.<string>, fn: Function}>}
+ */
this.formats_ = {};
/**
- * @private
- * @type {Object.<string, ?Object>}
- */
+ * @private
+ * @type {Object.<string, ?Object>}
+ */
this.prev_ = {};
/**
@@ -98,9 +98,9 @@ mvc.Model = function(opt_options) {
this.changeHandler_ = goog.events.listen(this,
goog.events.EventType.CHANGE, this.change_, false, this);
/**
- * @private
- * @type {string}
- */
+ * @private
+ * @type {string}
+ */
this.cid_ = '' + goog.getUid(this);
this.changing_ = [false];
@@ -122,10 +122,10 @@ goog.inherits(mvc.Model, goog.events.EventTarget);
*/
mvc.Model.Compare = {
/**
- * @param {*} a first object to compare.
- * @param {*} b second object to compare.
- * @return {boolean} whether they are equal.
- */
+ * @param {*} a first object to compare.
+ * @param {*} b second object to compare.
+ * @return {boolean} whether they are equal.
+ */
RECURSIVE: function(a, b) {
// if a and b are objects then recurse through keys and check values
@@ -151,10 +151,10 @@ mvc.Model.Compare = {
return a === b;
},
/**
- * @param {*} a first object to compare.
- * @param {*} b second object to compare.
- * @return {boolean} whether they are equal.
- */
+ * @param {*} a first object to compare.
+ * @param {*} b second object to compare.
+ * @return {boolean} whether they are equal.
+ */
STRING: function(a, b) {
try {
return a.toString() === b.toString();
@@ -163,10 +163,10 @@ mvc.Model.Compare = {
}
},
/**
- * @param {*} a first object to compare.
- * @param {*} b second object to compare.
- * @return {boolean} whether they are equal.
- */
+ * @param {*} a first object to compare.
+ * @param {*} b second object to compare.
+ * @return {boolean} whether they are equal.
+ */
SERIALIZE: function(a, b) {
return goog.json.serialize(a) === goog.json.serialize(b);
}
@@ -256,10 +256,20 @@ mvc.Model.create = function(opt_options) {
*/
mvc.Model.prototype.toJson = function() {
var json = goog.object.clone(this.attr_);
- goog.array.forEach(goog.object.getKeys(json), function(key) {
- if (!goog.isDef(json[key]))
- delete json[key];
- });
+
+ var recurseRemove = function(obj) {
+ if (!goog.isObject(obj))
+ return;
+ goog.array.forEach(goog.object.getKeys(obj), function(key) {
+ if (!goog.isDef(obj[key]))
+ delete obj[key];
+ else if (goog.isObject(obj[key]))
+ recurseRemove(obj[key]);
+ });
+ };
+
+ recurseRemove(json);
+
return json;
};
@@ -316,8 +326,10 @@ mvc.Model.prototype.get = function(key, opt_default) {
},this));
return goog.isDef(get) ? get : opt_default;
}
+ if (key.indexOf('.') < 0)
+ return goog.isDef(this.attr_[key]) ? this.attr_[key] : opt_default;
var path = key.split('.').reverse();
- var obj = this.attr_;
+ var obj = this.get(path.pop());
while (path.length && goog.isDef(obj))
obj = obj[path.pop()];
return goog.isDef(obj) ? obj : opt_default;
@@ -405,11 +417,11 @@ mvc.Model.prototype.set = function(key, opt_val, opt_silent) {
// for each key:value try to set using schema else set directly
goog.object.forEach(key, function(val, key) {
- if (!goog.isDef(val)) {
- this.attr_[key] = val;
- if (this.prev_[key] != this.attr_[key])
- success = true;
- } else {
+ // if (!goog.isDef(val)) {
+ // this.attr_[key] = val;
+ // if (this.prev_[key] != this.attr_[key])
+ // success = true;
+ // } else {
try {
if (this.schema_[key] && this.schema_[key].set) {
var temp = goog.bind(
@@ -447,7 +459,7 @@ mvc.Model.prototype.set = function(key, opt_val, opt_silent) {
else
throw err;
}
- }
+ // }
}, this);
// if it was successful and not silent then fire change and then set
@@ -468,12 +480,22 @@ mvc.Model.prototype.set = function(key, opt_val, opt_silent) {
/**
- * @param {string} key to remove (calls to it's get return undefined).
+ * @param {Array|string|boolean=} opt_key to remove (calls to it's get return undefined).
* @param {boolean=} opt_silent true if no change event should be fired.
* @return {boolean} return if succesful.
*/
-mvc.Model.prototype.unset = function(key, opt_silent) {
- return this.set(key, undefined, opt_silent);
+mvc.Model.prototype.unset = function(opt_key, opt_silent) {
+ if (goog.isString(opt_key))
+ return this.set(opt_key, undefined, opt_silent);
+ if (!goog.isArray(opt_key)) {
+ opt_key = goog.object.getKeys(this.attr_);
+ }
+ var temp = {};
+ goog.array.forEach(opt_key, function(k) {
+ temp[k] = undefined;
+ })
+ return this.set(temp, opt_key);
+
};
View
4 router.js
@@ -63,8 +63,8 @@ goog.inherits(mvc.Router, goog.events.EventTarget);
*/
mvc.Router.EventType = {
/*
- * event to trigger when route is about to change.
- */
+ * event to trigger when route is about to change.
+ */
ROUTE_EXPIRED: "routeExpired"
}
Please sign in to comment.
Something went wrong with that request. Please try again.