Eugen Istoc
angular.module('app').controller('Ctrl1', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/posts').then(function(posts){
$scope.posts = posts;
});
$http.get('/comments').then(function(comments){
$scope.users = comments;
});
});angular.module('app').controller('Ctrl2', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/profiles').then(function(users){
$scope.profiles = profiles;
});
});angular.module('app').controller('Ctrl1', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/posts').then(function(posts){
$scope.posts = posts
});
$http.get('/comments').then(function(comments){
$scope.comments = comments;
});
});
angular.module('app').controller('Ctrl2', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/profiles').then(function(profiles){
$scope.profiles = profiles
});
});
angular.module('app').controller('Ctrl3', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/comments').then(function(comments){
$scope.comments = comments;
});
});
angular.module('app').controller('Ctrl5', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/comments').then(function(comments){
$scope.comments = comments;
});
$http.get('/profiles').then(function(profiles){
$scope.profiles = profiles
});
});Common Solution: Services
angular.module('app').controller('Ctrl1', function ($scope, $http) {
$http.get('/users').then(function(users){
$scope.users = users;
});
$http.get('/posts').then(function(posts){
$scope.posts = posts
});
$http.get('/comments').then(function(comments){
$scope.comments = comments;
});
});angular.module('app').controller('Ctrl1', function ($scope, UesrService, PostService, CommentService) {
UserService.get().then(function(users){
$scope.users = users;
});
PostService.get().then(function(posts){
$scope.posts = posts;
});
CommentService.get().then(function(comments){
$scope.comments = comments;
});
});angular.module('app').service('EventService', function ($q, $http) {
this.getAll = function(){
return $http.get('/events');
}
this.getEventsAndLocations = function(){
var deferred = $q.defer(), eventsAndLocations = [];
// One to Many relations (One location can have multiple events)
$http.get('/events').then(function(events){
events.forEach(function(event){
$http.get('/locations', {id: event.Location_ID}).then(function(location){
event.location = location
eventsAndLocation.push(event);
});
});
deferred.resolve(eventsAndLocations);
});
return deferred.promise;
}
});
angular.module('app').service('EventService', function ($http) {
this.getAll = function(){
return $http.get('/events');
}
});
angular.module('app').service('EventService', function ($q, $http) {
this.getAll = function(){
return $http.get('/events');
}
this.getEventsAndLocations = function(){
...
}
this.getEventsAndEventDefinitionCodes = function(){
...
}
this.getPeopleInEvent = function(event_id){
...
}
this.getPatternsInEvent = function(event_id){
...
}
});
bower install js-data --saveDS.find(id).then(function(){...});
DS.findAll().then(function(){...});
DS.destroy(id).then(function(){...});
DS.create(data).then(function(){...});
DS.update(id, data).then(function(){...});
var Event = store.defineResource({
name: 'event',
relations: {
hasOne: {
location: {
localKey: 'Location_ID',
localField: 'location'
},
event: {
localKey: 'Next_Event_ID',
localField: 'next_event'
},
event_definition: {
localKey: 'Event_Definition_Code',
localField: 'event_definition'
}
}
}
});var Location = store.defineResource({
name: 'location',
relations: {
belongsTo: {
event: {
foreignKey: 'Location_ID',
}
}
}
});var EventDefinition = store.defineResource({
name: 'event_definition',
relations: {
belongsTo: {
event: {
foreignKey: 'Event_Definition_code',
}
}
}
});Event.findAll() // http://www.example.com/eventsEvent.find(1) // http://www.example.com/event/1Event.loadRelations(1, ['Location', 'event_definition'])Model Lifecycle Hooks
beforeCreate
beforeUpdate
afterUpdate
afterCreate
...etc
Relations
belongsTo
hasMany
hasOne
var User = store.DefineResource({
name: 'user',
actions: {
fooStaticMethod: {
method: 'POST'
}
}
});
User.barStaticMethod = function () {...};
User.fooStaticMethod();
User.barStaticMethod();Static Methods
Instance Methods
var User = store.DefineResource({
name: 'user',
methods: {
say: function () {
return this.name;
}
}
});
var user = User.createInstance({
name: 'John'
});
user.say(); // "John"var User = store.DefineResource({
name: 'user',
computed: {
fullName: ['first', 'last', function () {
return this.first + ' ' + this.last;
]
}
});
var user = User.inject({
id: 1,
first: 'John',
last: 'Anderson'
});
user.fullName; // "John Anderson"Computed Properties
var User = store.DefineResource('user'});
User.filter({
where: {
age: {
'>': 18
}
}
}); // [{ id: 1, age: 30 }]
User.filter({
age: 15
}); // [{id: 1,age: 15},{id: 3,age: 15}]Queries