npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-resource-sails

v1.2.2

Published

Angular $resource style service for accessing sails socket.io resources

Downloads

10

Readme

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

Install 'angular-resource-sails' via either npm or bower and then and the following to your page head:

<script src="/assets/js/dependencies/sails.io.js"></script>
<script src="/node_modules/angular-resource-sails/src/sailsResource.js"></script>

(note that sails.io.js is a dependency you'll need to include before sailsResource, it's in the assets/js/dependencies folder by default)

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
  });

Callbacks also available for actions without parameters.

item.$save(
	function(item) {
		// Handle success
	},
	function(error) {
		// Handle error
	});

###Customize actions Default actions are get, query, save, and remove. You can override these or add your own.

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, function transformResponse(response) {
			// runs after response returns
			return someCustomLogicToRun(response);
		}},
		// attach a transformRequest function
		// overrides default $save function
		'save': { method: 'POST', function transformRequest(request) {
			// runs before request is made
			return JSON.stringify(someCustomLogicToRun(request));
		}}
	}
};

###Options We've included a few useful options you can enable on any sailsResource.

var service = sailsResource('item', {}, {
		verbose: true, // sailsResource will log messages to console
		prefix: 'myapi', // apply a prefix to all routes
		socket: socketInstance // provide your own socket instance,
		origin: 'http://notlocalhost.com' // change the socket origin
	}
};

You can also change these globally by editing the provider's configuration.

angular.module('myapp').config(function (sailsResourceProvider) {
	sailsResourceProvider.configuration = {
		prefix = 'api',
		verbose: true
	};
});

Realtime updates

All angular-resource-sails instances will be subscribed to socket.io updates. If the client receives 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
	}
});

You can also subscribe to events on collections inside models. The events names are :

  • $sailsResourceAddedTo, when an element has been added in a collection
  • $sailsResourceRemovedFrom when an element has been removed from a collection

Development

  1. bower install

Run Tests

  1. Open SpecRunner.html