🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

angular-models

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-models

Backbone-style models and collections for Angular!

0.1.4
latest
Source
npm
Version published
Weekly downloads
8
300%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status

Angular Models

Backbone-style models for Angular. If you're like me, one of the things you miss working with Angular is the lack of clearly defined models and collections which you can pass around to various parts of your app, attach custom methods to, call fetch on to refresh data, etc. I didn't find anything to my needs so I built a simple implementation of backbone models.

How to use

Check out the examples directory for a full working implementation with comments

  • Add a script tag for underscore.js (http://underscorejs.org/) and angular-models.js
  • Add 'angular-models' as a dependency of your app or module:
angular.module( 'myApp', ['angular-models']);
  • Inject 'Model' or 'Collection' and extend it just you would a backbone model giving it a 'urlRoot' for the model or 'url' for the collection just as in Backbone. See backbone (http://backbonejs.org) for more info:
.factory('MyModel', function(Model){
  return Model.extend({
	urlRoot: '/api/mymodel'
  });
  • Now make an instance of the model and call .fetch(), .save(), .toJSON() or other methods as with Backbone.
var model = new MyModel({ id: 400 });
model.fetch();
model.set({ name: 'test' });
model.save();
console.log(model.toJSON());

A couple gotchas

  • The model's attributes are stored as model.atts (shorthand for model.attributes) so your template will need to reference model.atts not just model:
$scope.model = model;
now in the template this will not work:
<p>{{model.title}}</p>
but this will:
<p>{{model.atts.title}}</p>
  • similarly a collections models are stored as collection.models so things like ng-repeat must reference the collection.models:
$scope.collection = collection;
now in the template:
<p ng-repeat="model in collection.models">{{model.attributes.title}}</p>

To do:

Want to help? Yes!

  • achieve better parity and consistency with how Backbone methods, arguments, etc.
  • better test coverage
  • remove dependency on underscore by using angular built in methods
  • clean up
  • better docs and instructions

FAQs

Package last updated on 25 Feb 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts