Skip to content

Instantly share code, notes, and snippets.

@awilson28
Last active January 7, 2016 16:24
Show Gist options
  • Save awilson28/414234f746796ebb2a36 to your computer and use it in GitHub Desktop.
Save awilson28/414234f746796ebb2a36 to your computer and use it in GitHub Desktop.
  • STEP 1: npm install js-data and js-data-angular in that order
  • STEP 2: list js-data as a dependency and set default values (DSProvider is the name of the DS object in the config step):
var app = angular.module('nameOfApp', ['js-data'])
  .config(function(DSProvider) {
  
    DSProvider.defaults.basePath = '/api';
    DSProvider.defaults.idAttribute = '_id';
    
  })
  • STEP 3: inject the DS object into a factory
  app.factory('Post', function(DS) { })
  • STEP 4: start building factories
app.factory('Post', function(DS, $state) {
	
	var Post = DS.defineResource({
		name: 'posts', 
		relations: {
			belongsTo: {
				users: {
					localKey: 'author', 
					localField: '_author'
				}
			}
		}, 
		methods: {
			go: function(){
				$state.go('post', {
					postId: this._id, 
					authorId: this.author
				})
			}
		}
	})

	return Post; 
}).run(function (Post) {})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment