Giter VIP home page Giter VIP logo

angular-resource-sails's Introduction

angular-resource-sails

Sails.js is a realtime MVC framework built for NodeJS. Angular is a front end framework for building client side web applications. One of Angular's features is ngResource, a small service that allows developers to create objects on the client that can be saved and deleted as if they were on the server. It's a fantastic abstraction that eliminates the usual plumping code of making a change on the client, doing an HTTP call, and handling the response. However, it only works with the Angular $http service, which does not operate using socket.io as Sails.js does.

angular-resource-sails bridges this gap by allowing you to create service objects that behave like those from ngResource but do all of their updating through Sails.js socket.io connections. It also sets up the binding necessary for realtime updates to affect the client.

Install

Add 'angular-resource-sails' and 'sails.io.js' to your bower.json file. Include both in your page somewhere. Example:

<script src="/bower_components/sails.io.js/dist/sails.io.js"></script>
<script src="/bower_components/angular-resource-sails/src/sailsResource.js"></script>

Then, in your Angular application dependencies include 'sailsResource' as one of them.

angular.module('myApp', ['sailsResource']);

Usage

###Create model instance

var Item = sailsResource('item');
var newItem = new Item();
newItem.data = 'abc';
newItem.$save(); // POST /item (if the item does not have an id)

###Get a listing of model instances

var items = sailsResource('item').query(); // GET /item

###Get a single model instance

var item = sailsResource('item').get({ id: 53 }); // GET /item/53

###Update a model instance

var item = sailsResource('item').get({ id: 53 });
item.data = 'def';
item.$save(); // PUT /item/53 (if the item has an id)

###Delete a model instance

var item = sailsResource('item').get({ id: 53 });
item.$delete(); // DELETE /item/53

###Success and error callbacks Works like ngResource - can optionally provide callbacks

var item = sailsResource('item').get({ id: 'notreal' }, 
  function(response) { // first function is success handler
    // Handle success
  },
  function(response) { // second function is error handler
    // Handle error
  });

###Customize actions Works like ngResource

var service = sailsResource('item',
	{
		// create a custom PUT
		'update' { method: 'PUT' }, // Resources will have $update function
		// attach a transformResponse function
		// overrides default query function
		'query': { method: 'GET', isArray: true, transformResponse(response) {
			// runs after response returns
			return someCustomLogicToRun(response);
		}},
		// attach a transformRequest function
		// overrides default $save function
		'save': { method: 'POST', transformRequest(request) {
			// runs before request is made
			return JSON.stringify(someCustomLogicToRun(request));
		}}
	}
};
// More customizations to come!

Realtime updates

All angular-resource-sails instances will be subscribed to socket.io updates. If the client recieves a create, update, or delete message from the server every instance already created will automatically update as needed.

Additionally, angular-resource-sails will $broadcast a $sailsResourceCreated, $sailsResourceUpdated, and $sailsResourceDestroyed messages when those socket messages are received. You can respond in a customized way by subscribing to those events.

$rootScope.$on('$sailsResourceUpdated', function(event, message) {
	if(message.model == 'user') {
		// some logic for user update messages
	}
});

Development

  1. bower install

Run Tests

  1. Open SpecRunner.html

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.