Code Monkey home page Code Monkey logo

ng-backbone's Introduction

ng-backbone

Backbone data model and collection for AngularJS

GitHub release Build Status Coverage Status

David David

Dependencies

Backbone factory

To make Backbone work properly with AngularJS, ng-backbone overrides Backbone's sync and ajax methods.

NgBackboneModel

Base NgBackbone model extends Backbone.model by adding additional properties and functions, including $attributes and $status. When overriding NgBackboneModel set method but you would like to keep $attributes, you'll have to explicitly call NgBackboneModel set:

var Sample = NgBackboneModel.extend({  
  set: function(key, val, options) {  
    NgBackboneModel.prototype.set.apply(this, arguments);  
  }  
});  

In rare cases when you want to override the constructor which allows you to replace the actual constructor function for your model, you should invoke NgBackboneModel constructor in the end.

var Sample = NgBackboneModel.extend({  
  constructor: function() {  
    this.text = 'Sample!';  
    NgBackboneModel.apply(this, arguments);  
  }  
});  

The $attributes property allows application to use AngularJS two-way binding to manipulate Backbone objects using Backbone get and set.
HTML:

<input type="text" ng-model="person.$attributes.name">  

Javascript:

$scope.person = new Person({  
  name: 'John'  
});  

The $status property is the hash containing model sync state. Since $status updates using Backbone event, passing {silent: true} will prevent $status from updating. $status contains four properties, including:

  • deleting: Set to true when invoking destroy method on model (HTTP DELETE request)
  • loading: Set to true when fetching model data from server (HTTP GET request)
  • saving: Set to true when creating or updating model (HTTP POST or PUT request)
  • syncing: Set to true whenever a model has started a request to the server

HTML:

<span ng-if="user.$status.loading">Loading</span>  
<label>{{user.name}}</label>  

Javascript:

$scope.user = new User({id: '123'});  
$scope.user.fetch();  

$resetStatus

Reset all properties on $status including deleting, loading, saving, and syncing back to false

$setStatus

Update model status on $status

Parameters

attributes
Type: Object
Set one or multiple statuses

options
Type: Object
Options

NgBackboneCollection

Base NgBackbone collection extends Backbone.collection by adding additonal properties and functions, such as $models and $status.

Similar to NgBackboneModel, in rare cases where you may want to override the constructor, you should invoke NgBackboneCollection in the end.

var SampleCollection = NgBackboneCollection.extend({  
  constructor: function(models, options) {  
    this.allSamples = false;  

    NgBackboneCollection.apply(this, arguments);  
  }  
});  

The $models property creates a one-way binding to collection models which is the Javascript array of models. Application can only access the array with $models but will not be able to modify it.
HTML:

<ul>  
  <li ng-repeat="user in users.$models">{{user.username}}<li>  
</ul>  

Javascript:

$scope.users = new Users();  
$scope.users.fetch();  

The $status property is the hash containing collection and its models sync state. Since $status updates using Backbone event, passing {silent: true} will prevent $status from updating. $status contains four properties, including:

  • deleting: Set to true when one of its models is getting destroyed (HTTP DELETE request)
  • loading: Set to true when fetching collection data from server (HTTP GET request)
  • saving: Set to true when creating or updating one of its models (HTTP POST or PUT request)
  • syncing: Set to true whenever a collection has started a request to the server

HTML:

<ul>  
  <li ng-if="users.$status.loading">Loading...</li>  
  <li ng-repeat="user in users.$models">{{user.username}}<li>  
</ul>  

Javascript:

$scope.users = new Users();  
$scope.users.fetch();  

$setStatus

Update collection status

Type: function

Parameters

attributes
Type: Object
Set on or multiple statuses

options
Type: Object
Options

$resetStatus

Reset all statuses including deleting, loading, saving, and syncing back to false

Type: function

ng-backbone's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ng-backbone's Issues

filtering leads to an endless loop

Thanks for the great wrapper!

How did you get it to work with filtering? For me it results in an endless loop (maximum call stack size exceeded, inside of the search of the angular filter). Which probably is due to the fact that filter tries to iterate every property of the object it's supposed to filter. In the case of Backbone models those properties are in some cases transitively self-referencing (model.collection, xhr,...). Any ideas?

RelationShip

How to create and get RelationShip beetwen models?

HTTP response code not available in callback functions

For success and error callbacks, you get the following arguments: (collection, response, options)

In the normal Backbone + $.ajax world, this callback function gets passed to $.ajax, where the first argument is data (the data response from the server, parsed according to its dataType). Data then gets passed on to be the response argument of our callback.

At the same time, the jqXHR object returned by $.ajax gets assigned to options.xhr, so you are able to look at the response code from there. In contrast with current ng-backbone, options.xhr is just the promise returned by $http.

To make things more confusing, $.ajax's error callback gets a jqXHR for its first argument...so while backbone's docs say that both callbacks get the same arguments (collection, response, options), response is different for error...and you can grab the HTTP status code from it.

Would you accept a PR that exposes the HTTP status code in some way? What if we did something like this...

var xhr = ajax(_.extend(params, options)).
  success(function(data, status, headers, config) {
    options.xhr = {
      status: status
      headers: headers
      config: config
    }

    if (!isUndefined(options.success) && _.isFunction(options.success)) {
      options.success(data);
    }
  }).
  error(function(data, status, headers, config) {
    options.xhr = {
      status: status
      headers: headers
      config: config
    }

    if (!isUndefined(options.error) && _.isFunction(options.error)) {
      options.error(data);
    }
  });

options.xhr still wouldn't be an xhr object ($http doesn't give us that), but it would be a little bit closer. Currently options.xhr is just the same promise that already gets returned by the fetch, create, etc. functions.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.