diegopamio / angular-sails-bind Goto Github PK
View Code? Open in Web Editor NEWAn angular service to bind scope models to sailsjs models thru socket
License: MIT License
An angular service to bind scope models to sailsjs models thru socket
License: MIT License
Hey, how do you think, the same lib can be done with Laravel? I mean Laravel is very similiar to Sails.
Hey,
So I'm having a little trouble with altering JSON objects in a MySQL database via the RESTapi, when I change a value it will update the bound scope object, but it will change it into a JSON string, then if I refresh the page it will actually parse the JSON correctly.
example:
table:
animal
id:int(ai) | name:string | interests:json
1 | Mr. Sniffles | ["mewing","taking over the world","catnip"]
So the in my controller this will get me a object:
$scope.animals
[{
id: 1,
name: "Mr. Sniffles",
interests: [
"mewing",
"taking over the world",
"catnip"
]
}]
Which is great, but now if I HTTP PUT to /animal/1
interests : '["mewing","catnip"]'
It will change the scope object into:
[{
id: 1,
name: "Mr. Sniffles",
interests: '["mewing","catnip"]' // (string)
}]
Then if I refresh the page it will change it into:
[{
id: 1,
name: "Mr. Sniffles",
interests: [
"mewing",
"catnip"
]
}]
Thanks in advance,
How would you detect the 'Model Updated' events within the controllers?
The idea being that you could have the server 'do something' each time a model entry is updated.
Hi,
In my controller I have this,
$sailsBind.bind('model, $scope, $scope.pagination);
When I change $scope.pagination
in my controller, the change isn't passed on to the socket. Cant redeclare $sailsBind.bind()
because it breaks the model in sailsjs and eventually I end up with an empty model.
Cheers,
Ash
We've just noticed $apply() isn't called when receiving a model created message from Sails. I've only just started using the module, so haven't tested any other events. We're using autoWatch:true.
Angular 1.0.8
Sails 0.10.0
I've fixed this on my local machine. Let me know if you want a pull request.
I'm testing Angular Sails Bind, and it is really close to being super powerful in my app. I'm wondering if there is a way (or if it should) work off sub-items. That is, imagine I have A's and B's with an N-to-N relationship. I query for one A and get it back, and it has three B's attached to it. If I could watch items in B and manipulate them, it would be super powerful. I imagine others would have a benefit with this also.
Is this a good feature request? Useful? Doable?
Thanks,
Ryan
The standard method of loading content using $http in Angular has the option to specify a success callback when the content has loaded, so that in the controller, you can initiate scope bindings dependent on data.
The angular-sails-bind directive has no such thing. I implemented it myself in arryon@0ec4907 but probably there is a better way to do this, like the default .success() and .error() paradigm. Any ideas?
I am trying to integrate this with my angular project but as soon as I make a call inside a controller I get the following error:
Error: io.socket is undefined
_get@http://localhost:9000/bower_components/angular-sails-bind/angular-sails-bind.js:179:13
I have confirmed that sails.io.js is being included before angular etc etc, any ideas?
Would be great to have this on npm.
Has this been yet tested with Sails v0.10?
Any known issues?
Is there a discussion board for this lib?
This is really great I gotta say.
http://imageshack.com/f/ipGZp3J8p
Please, help me because I really like your stuff but I cant make it work ;(
This might be an issue with Sails, or my configuration.
However in my controller I am binding:
$sailsBind.bind("user", $scope)
$sailsBind.bind("task", $scope)
When I am using the register function on the controller updates are not being sent. I can see socket messages being pushed to the task object.
the register function is being called via HTTP POST instead of a socket call.
Task works, user does not.
User model:
var User = {
// Enforce model schema in the case of schemaless databases
schema: true,
attributes: {
groups: {
collection: 'groups',
via: 'members',
required: true
},
username : { type: 'string', unique: true, required: true },
email : { type: 'email', unique: true, required: true },
passports : { collection: 'Passport', via: 'user' }
// Groups
}
};
module.exports = User;
User controller:
/**
* UserController
*
* @description :: Server-side logic for managing Users
* @help :: See http://links.sailsjs.org/docs/controllers
*/
module.exports = {
session: function(req, res) {
res.json({
success: true
});
},
//expose a method for angular to be able to see if we are logged in
isLoggedIn: function(req,res){
//TODO: make sure that only the clients with the right session get the broadcast
//sails.sockets.broadcast("authNotification" , "authorized",{socketId: req.socket.id, user: req.user} );
res.json({sessionID: req.session.sessionID, roles: req.session.roles, user: req.user} );
//res.json(req.isAuthenticated() ? {sessionID: req.sessionID, user: req.user} : 0);
},
//expose a method for angular to be able to see if we are logged in
sayHi: function(req,res){
sails.sockets.broadcast("authNotification" , "message", {message: "onLogin"} );
sails.sockets.broadcast("authNotification" , "message", {message: "hello3"} );
res.json({result:"sent"});
},
sendHello: function(req,res) {
//req.sessionID
sails.io.sockets.in('asdf').emit("eventName", {data:true});
//sails.sockets.broadcast.to("asdf").emit('loggedOut', {loggedOff: 'true'});
res.json({
sessionid: req.sessionID,
message: "sent"
})
},
register: function(req,res){
//associate group
Groups.findOrCreate({
groupName: 'user'
}, {
groupName: 'user',
description: 'Generic user'
}).exec(function createFindCB(err, userGroup) {
//Check password for equality
var password = req.param('password');
var confirmation = req.param('confirmation');
//TODO: enfore better passsword enforcement
if(!password){
res.json({err: { message: "You must enter a password" }});
} else if (password == confirmation){
var userObj = {
username: req.param('username'),
email: req.param('email'),
groups: [userGroup.id]
};
//Create user
User.create(userObj, function userCreated(err, user) {
if (err) {
res.json({
err: err
});
} else {
//Associate passport ( password )
//TODO: include other authentication mechanisms
Passport.create({
protocol: 'local',
password: password,
user: user.id
}, function (err, passport) {
if (err){
User.destroy(user.id, function deleteCB(deleteErr){
sails.log(deleteErr);
});
res.json({err:err});
console.log(err);
} else {
res.json({success: true});
}
});
}
});
} else {
res.json({err: { message: "Passwords do not match" }});
}
});
}
};
I have policies that reject unauthorized requests to certain resources. It is quite easy to catch and handle if I'm using the REST API.
I can't find a good way of doing this with this module yet.
Is it possible to set headers for the socket requests?
As i would like to be able to set a Auth header on each request?
If I have something like
tree.rings
and I...
$sailsBind.bind("tree", $scope)
The trees are two-way binding, but the rings are not.
Diego thank you very much for sharing this.
Would you mind adding to the readme a snippet showing how to specify the URL for the sailsjs resource?
Thank you
Currently, the routes used are the blueprint routes generated by the config option for shortcuts, rather than the routes generated from the config option for REST. The REST routes are a little bit more standard / secure, and the behavior is more consistent when using them.
So for the create/update/and delete actions done, these routes and verbs should be updated appropriately.
Using a Sails model "item" that specifies "runtimeData" as a JSON object, I have not been able to get this to work when runtimeData is already set to a value. For instance, given this data existent in the MongoDB data store:
$scope.items[0].runtimeData = { "currentVal": 1 };
The following does not trigger an update:
$scope.items[0].runtimeData.currentVal = 2;
And this rarely / unreliably triggers an update:
$scope.items[0].runtimeData = { "currentVal" : 2 };
But this always triggers:
$scope.items[0].runtimeData = { "lastUpdate" : new Date().getTime(), "currentVal" : 2 };
It seems that a combination of changing the entire runtimeData object AND including a value is what is causing the update to be performed properly. All bindings at the root level ($scope.items[0].customVal = "5") work great at mirroring back to the database.
Any ideas? I haven't dived into the code enough to know how to fix, and I am new to Angular.
Thanks!
Ryan
The model collection seems to be initing just fine. However, when I use another browser window to PUT changes to the model, the moment I submit those changes I get this error on the front end of the app...
I'm logging out the section of angular-sails-bind.js that is erring out.
Seems like I'm almost there, but updates is breaking the front end.
I have an angularjs app running on port 3131. This is what I get in the console.
GET XHR http://127.0.0.1:3131/socket.io/ [HTTP/1.1 404 Not Found 2ms]
"
Socket is trying to reconnect to Sails...
_-|>_- (attempt #4)
"
So I've tried to change the url but the output stayed the same
io.sails.url = 'http://localhost:1337';
Is there a way to achieve this?
Thank you
I've hacked a fix using the deep watch syntax in my controller:
$scope.$watchCollection = function(variable, cb) {
return $scope.$watch(variable, cb, true);
};
Could we replace $watchCollection with $watch(resourceName + "s", callback, true)
?
Getting various errors in Firefox using the firebug console:
Error: model.forEach is not a function
addCollectionWatchersToSubitemsOf@https://10.0.2.204/js/bower_components/angular-
sails-bind/dist/angular-sails-bind.js:120:7 bind/<@https://10.0.2.204
/js/bower_components/angular-sails-bind/dist/angular-sails-bind.js:45:9
Re/e/l.promise.then/J@https://10.0.2.204/js/angular.min.js:101:1
Along with others. Not really sure what could be causing it as I am mostly a newb but just thought I would throw it on here in case anyone else was having the same problem or had some input. Working fine in Chrome. If theres any useful info you want from me I can try to provide it, but we aren't doing anything particularly special.
hello there
i used
$sailsBind.bind("resource", $scope);
in my project to listen to realtime updates via socket of resource model, but it only display the first 30 records, like it was paginating the results. This a default behavior or some king of pagination that sails do??
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.