On Github niquola / angular-in-nutshell
@ waveaccess/choice-hospital-systems
(Presentation Model) framework for single page applications (SPA)
vs
(Presentation Model) framework for SPA
Making Decisions
DHH
Best Practices
by Inversion of Control
Presentation Model framework for single page applications
Data Bindings
Presentation Model framework for single page applications
angular.module('a', [])
.service('aserv',...)
.directive('adir',...)
.controller(...)
.filter(...)
angular.module('b', ['a'])
.controller(function(adir){ // here is injection
//use adir
})
//
angular.module('app', [])
.controller('userCtrl',function($scope){
$scope.users = [{ name: 'Misko' }, {name: 'Voita'}, {name: 'Minar'}]
})
//view
html ng-app="app">
...
$('#input').on('input', function(){
$('#out').text($(this).value());
})
var DocumentRow = Backbone.View.extend({
tagName: "li",
className: "document-row",
events: {
"change .input": "input"
},
initialize: function() {
this.listenTo(this.model, "change", this.render);
}
render: function() {... }
input: function() {... }
});
Find more generic way (DRY)
Just say "WHAT TO DO", not "HOW TO DO".
{{message}}
machine should do it for me!
//directives
function model(el, scope) {...}
function interpolate(el, scope) {...}
//complie document
var scope = {} //container for data
$("[ng-model]")
.each(function(el){ model(el, scope) })
$("*:contains('{{')")
.each(function(el){ interpolate(el, scope) })
function interpolate(el, scope) {
var template = el.innerHTML; //get & remember template
var prop = /{{(.+)}}/.exec(template)[1];
//imagine we have such method,
//calling callback when prop changed
scope.$watch(prop, function() {
console.log(scope)
el.innerHTML = template.replace('{{' + prop +'}}', scope[prop]);
})
}
ng-interpolate.js
function model(el, scope) {
var model = el.getAttribute('ng-model')
$(el).on('input',function(){
var data = el.value;
scope.apply(function(){
scope[model] = data;
})
})
scope.watch(model, function(val) {
el.value = scope[model];
})
}
ng-input.js
// Scope
$watch: function(watchExp, listener,){
this.$$watchers.push(watchExp, listener)
}
$digest: function() {
var dirty = true;
while(dirty || iteration > limit) {
for $watcher in $$watchers {
var newValue = eval($watcher.watchExp)
if($watcher.oldValue != newValue){
$watcher.listener(newValue, oldValue)
}
}
}
}
rootScope.js