Skip to content

Commit

Permalink
fixed some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
zivc committed Nov 25, 2014
1 parent faa22c8 commit 8e345c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ng-sails
Installation
------------

Include `ng-sails.js` (or `bower install ng-sails`) file into your project and don't forget to add the `ngSails` module to your modules dependencies.
Include `ng-sails.js` (or `npm install ng-sails`) file into your project and don't forget to add the `ngSails` module to your apps dependencies.

In your controller, require `$sails` and then bind the models together like so:

Expand All @@ -15,16 +15,38 @@ In your controller, require `$sails` and then bind the models together like so:
sort:"id desc"
});

This will establish a socket.io socket and update `$scope.model` whenever CRUD events are fired over the socket from sails.
This will establish a socket.io socket and update `$scope.model` whenever CRUD events are fired over the socket from sails. Changing the sort order from `"id desc"` will stop `create` events from automatically appending to the top of your model. All other events will update the model with the changes should that item exist.

The third parameter is the same as the [waterline queries](https://github.com/balderdashy/waterline-docs/blob/master/query.md).

Your actual sails model will now exist inside `$scope.model.data`. Modifying any of the `$scope.model.params` values will re-establish socket connections with those new values so be mindful when binding the model directly to an `input[type="text"]`.

You can now use full CRUD methods on the model after it has been bound

$scope.model.create({name:"Ash"});

The other CRUD methods require an ID to be the first parameter

$scope.model.update(1, {name:"Ash"});

The methods are `create`, `retrieve`, `update` and `destroy`.

You could always hook into the `$scope.model.crud()` method directly. It takes three parameters, the first being either `get`, `post`, `put` or `delete`, second being the object and third is the optional id. __This is subject to change to support waterline ORM in the future__.

CRUD methods return promises instead of itself so you can't chain them unfortunately.

Pagination
----------
A quick and easy way to enable pagination is to modify the `params` object of the model returned in your angular scope.

$scope.model.params.skip = 30;

This will load the next set of results.

Bugs
----------
There is currently no way to remove named sockets with the current version of `sails.io.js`, I don't think :S

Maintainer
----------
Ash Taylor, [[email protected]](mailto:[email protected])
1 change: 1 addition & 0 deletions ng-sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ angular.module('ngSails', []).factory('$sails', ['$q',
$sails.prototype.crud = function(method, object, id) {
var q = $q.defer();
object = object || {};
method = (typeof method == "string" ? method : 'get').toLowerCase();
io.socket[method](this.prefix+this.model+(id ? '/'+id : ''), object, function(data,response) {
var verb = false;
switch (method) {
Expand Down
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "ng-sails",
"version": "0.1.0",
"description": "Bind a sails model to an Angular model with full CRUD support",
"repository": {
"type": "git",
"url": "https://github.com/zivc/ng-sails.git"
},
"keywords": [
"sailsjs",
"angularjs",
"sails",
"angular",
"socket",
"socket.io"
],
"author": "Ash @zivcjs <[email protected]> (http://zi.vc/)",
"license": "ISC",
"bugs": {
"url": "https://github.com/zivc/ng-sails/issues"
},
"homepage": "https://github.com/zivc/ng-sails"
}

0 comments on commit 8e345c6

Please sign in to comment.