All Projects → afeld → Backbone Nested

afeld / Backbone Nested

Licence: mit
A plugin to make Backbone.js keep track of nested attributes - looking for maintainers! https://github.com/afeld/backbone-nested/issues/157

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Backbone Nested

NestedReact
BackboneJS compatibility layer for React-MVx MVVM framework.
Stars: ✭ 79 (-82.56%)
Mutual labels:  backbone
backbone.marionette.example
MVVM-flavored example CRUD app with Backbone.Marionette + Backbone.stickit + Browserify
Stars: ✭ 37 (-91.83%)
Mutual labels:  backbone
backbone-wp
A WordPress theme built with Backbone that uses front-end routing.
Stars: ✭ 12 (-97.35%)
Mutual labels:  backbone
marionette.routing
State based router for MarionetteJS
Stars: ✭ 21 (-95.36%)
Mutual labels:  backbone
backbone-tutorial-series
Source code generated in the Backbone.js tutorial series
Stars: ✭ 71 (-84.33%)
Mutual labels:  backbone
general backbone
No description or website provided.
Stars: ✭ 37 (-91.83%)
Mutual labels:  backbone
modified refinedet
Modified RefineDet
Stars: ✭ 23 (-94.92%)
Mutual labels:  backbone
Pvt
Stars: ✭ 379 (-16.34%)
Mutual labels:  backbone
RosettaDesign
RosettaDesign using PyRosetta
Stars: ✭ 19 (-95.81%)
Mutual labels:  backbone
backbone.base-router
A better starting point for building a new Backbone Router.
Stars: ✭ 49 (-89.18%)
Mutual labels:  backbone
backbone.react-bridge
Transform Backbone views to React components and vice versa
Stars: ✭ 26 (-94.26%)
Mutual labels:  backbone
detectron2 backbone
detectron2 backbone: resnet18, efficientnet, hrnet, mobilenet v2, resnest, bifpn
Stars: ✭ 171 (-62.25%)
Mutual labels:  backbone
espresso.js
Super minimal MVC library
Stars: ✭ 521 (+15.01%)
Mutual labels:  backbone
backscatter
Reactive extension for Backbone
Stars: ✭ 17 (-96.25%)
Mutual labels:  backbone
Vie
Semantic Interaction Framework for JavaScript
Stars: ✭ 307 (-32.23%)
Mutual labels:  backbone
Awesome-Vision-Transformer-Collection
Variants of Vision Transformer and its downstream tasks
Stars: ✭ 124 (-72.63%)
Mutual labels:  backbone
backboneindex
Filter Backbone resources with ease
Stars: ✭ 15 (-96.69%)
Mutual labels:  backbone
Force
The Artsy.net website
Stars: ✭ 429 (-5.3%)
Mutual labels:  backbone
Autoedit 2
Fast text based video editing, node Electron Os X desktop app, with Backbone front end.
Stars: ✭ 343 (-24.28%)
Mutual labels:  backbone
flexible-yolov5
More readable and flexible yolov5 with more backbone(resnet, shufflenet, moblienet, efficientnet, hrnet, swin-transformer) and (cbam,dcn and so on), and tensorrt
Stars: ✭ 282 (-37.75%)
Mutual labels:  backbone

Backbone-Nested Build Status

A plugin to make Backbone.js keep track of nested attributes. Download the latest version and see the changelog/history/release notes on the Releases page. Supports Backbone 0.9.x and 1.x.

The Need

Suppose you have a Backbone Model with nested attributes, perhaps to remain consistent with your document-oriented database. Updating the nested attribute won't cause the model's "change" event to fire, which is confusing.

var user = new Backbone.Model({
  name: {
    first: 'Aidan',
    last: 'Feldman'
  }
});

user.bind('change', function(){
  // this is never reached!
});

user.get('name').first = 'Bob';
user.save();

Wouldn't it be awesome if you could do this?

user.bind('change:name.first', function(){ ... });

Installation

Bower

Recommended.

  1. Install the latest version:

    bower install backbone backbone-nested-model jquery underscore --save
    
  2. Add backbone-nested.js to your HTML <head>:

    <!-- must loaded in this order -->
    <script type="text/javascript" src="/bower_components/jquery/jquery.js"></script>
    <script type="text/javascript" src="/bower_components/underscore/underscore.js"></script>
    <script type="text/javascript" src="/bower_components/backbone/backbone.js"></script>
    <script type="text/javascript" src="/bower_components/backbone-nested-model/backbone-nested.js"></script>
    

Manual

Download the latest release and the dependencies listed above, then include with script tags in your HTML.

Usage

  1. Change your models to extend from Backbone.NestedModel, e.g.

    var Person = Backbone.Model.extend({ ... });
    
    // becomes
    
    var Person = Backbone.NestedModel.extend({ ... });
    
  2. Change your getters and setters to not access nested attributes directly, e.g.

    user.get('name').first = 'Bob';
    
    // becomes
    
    user.set({'name.first': 'Bob'});
    

Best of all, Backbone.NestedModel is designed to be a backwards-compatible, drop-in replacement of Backbone.Model, so the switch can be made painlessly.

Nested Attributes

get() and set() will work as before, but nested attributes should be accessed using the Backbone-Nested string syntax:

1-1

// dot syntax
user.set({
  'name.first': 'Bob',
  'name.middle.initial': 'H'
});
user.get('name.first') // returns 'Bob'
user.get('name.middle.initial') // returns 'H'

// object syntax
user.set({
  'name': {
    first: 'Barack',
    last: 'Obama'
  }
});

1-N

// object syntax
user.set({
  'addresses': [
    {city: 'Brooklyn', state: 'NY'},
    {city: 'Oak Park', state: 'IL'}
  ]
});
user.get('addresses[0].state') // returns 'NY'

// square bracket syntax
user.set({
  'addresses[1].state': 'MI'
});

Events

"change"

"change" events can be bound to nested attributes in the same way, and changing nested attributes will fire up the chain:

// all of these will fire when 'name.middle.initial' is set or changed
user.bind('change', function(model, newVal){ ... });
user.bind('change:name', function(model, newName){ ... });
user.bind('change:name.middle', function(model, newMiddleName){ ... });
user.bind('change:name.middle.initial', function(model, newInitial){ ... });

// all of these will fire when the first address is added or changed
user.bind('change', function(model, newVal){ ... });
user.bind('change:addresses', function(model, addrs){ ... });
user.bind('change:addresses[0]', function(model, newAddr){ ... });
user.bind('change:addresses[0].city', function(model, newCity){ ... });

"add" and "remove"

Additionally, nested arrays fire "add" and "remove" events:

user.bind('add:addresses', function(model, newAddr){ ... });
user.bind('remove:addresses', function(model, oldAddr){ ... });

Special Methods

add()

Acts like set(), but appends the item to the nested array. For example:

user.get('addresses').length; //=> 2
user.add('addresses', {
  city: 'Seattle',
  state: 'WA'
});
user.get('addresses').length; //=> 3

remove()

Acts like unset(), but if the unset item is an element in a nested array, the array will be compacted. For example:

user.get('addresses').length; //=> 2
user.remove('addresses[0]');
user.get('addresses').length; //=> 1

See also

Note, this plugin does not handle non-embedded relations (a.k.a. relations), which keeps it relatively simple. If you support for more complex relationships between models, see the Backbone plugin wiki page. There is also an open discussion about merging this project with backbone-deep-model.

Contributing

Pull requests are more than welcome - please add tests, which can be run by opening test/index.html. They can also be run from the command-line (requires PhantomJS):

$ npm install
$ grunt

See also: live tests for latest release.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].