Skip to content
This repository
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
11  README.md
Source Rendered
@@ -30,7 +30,7 @@ user.bind('change:name.first', function(){ ... });
30 30
 
31 31
 ## Usage
32 32
 
33  
-1. Download the latest version [here](https://github.com/afeld/backbone-nested/tags), and add `backbone-nested.js` to your HTML `<head>`, **after** `backbone.js` is included ([tested](http://afeld.github.com/backbone-nested/test/) against jQuery v1.7.1, Underscore v1.3.1 and Backbone v0.5.3).
  33
+1. Download the latest version [here](https://github.com/afeld/backbone-nested/tags), and add `backbone-nested.js` to your HTML `<head>`, **after** `backbone.js` is included ([tested](http://afeld.github.com/backbone-nested/test/) against [jQuery](http://jquery.com/) v1.7.1, [Underscore](http://documentcloud.github.com/underscore/) v1.3.1 and [Backbone](http://documentcloud.github.com/backbone/) v0.5.3).
34 34
 
35 35
     ```html
36 36
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
@@ -144,6 +144,15 @@ user.remove('addresses[0]');
144 144
 user.get('addresses').length; //=> 1
145 145
 ```
146 146
 
  147
+## Warnings
  148
+
  149
+Accessing a nested attribute will throw a warning in your console, because it's safer to use the getter syntax above.  To silence these warnings, add an argument of `{silent: true}` to `get()`:
  150
+
  151
+```javascript
  152
+user.get('addresses[0]'); // gives a warning in your console
  153
+user.get('addresses[0]', {silent:true}); // (silent)
  154
+```
  155
+
147 156
 ## Contributing
148 157
 
149 158
 Pull requests are more than welcome - please add tests, which can be run by opening test/index.html.
10  backbone-nested.js
@@ -12,7 +12,9 @@ Backbone.NestedModel = Backbone.Model.extend({
12 12
     Backbone.Model.prototype.constructor.apply( this, arguments );
13 13
   },
14 14
 
15  
-  get: function(attrStrOrPath){
  15
+  get: function(attrStrOrPath, opts){
  16
+    opts || (opts = {});
  17
+
16 18
     var attrPath = Backbone.NestedModel.attrPath(attrStrOrPath),
17 19
       childAttr = attrPath[0],
18 20
       result = Backbone.Model.prototype.get.call(this, childAttr);
@@ -28,7 +30,7 @@ Backbone.NestedModel = Backbone.Model.extend({
28 30
     }
29 31
 
30 32
     // check if the result is an Object, Array, etc.
31  
-    if (_.isObject(result) && window.console){
  33
+    if (!opts.silent && _.isObject(result) && window.console){
32 34
       window.console.log("Backbone-Nested syntax is preferred for accesing values of attribute '" + attrStrOrPath + "'.");
33 35
     }
34 36
     // else it's a leaf
@@ -38,7 +40,7 @@ Backbone.NestedModel = Backbone.Model.extend({
38 40
 
39 41
   has: function(attr){
40 42
     // for some reason this is not how Backbone.Model is implemented - it accesses the attributes object directly
41  
-    var result = this.get(attr);
  43
+    var result = this.get(attr, {silent: true});
42 44
     return !(result === null || _.isUndefined(result));
43 45
   },
44 46
 
@@ -70,7 +72,7 @@ Backbone.NestedModel = Backbone.Model.extend({
70 72
     var attrPath = Backbone.NestedModel.attrPath(attrStr);
71 73
     if (_.isNumber(_.last(attrPath))){
72 74
       var aryPath = _.initial(attrPath),
73  
-        childAry = this.get(aryPath);
  75
+        childAry = this.get(aryPath, {silent: true});
74 76
       
75 77
       // compact the array (remove falsy values)
76 78
       for (var i = 0; i < childAry.length; i++){

No commit comments for this range

Something went wrong with that request. Please try again.