On Github niquola / angular-basics-slides
Created by niquola / @niquola hospital-systems/waveaccess 2013
$('#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".
<input ng-model="message"> <p> {{message}} </p>
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
function Scope() { this.$$watchers = null this.$$asyncQueue = []; ... } Scope.prototype = { $watch: function(watchExp, listener,){...} $watchCollection: function(obj, listener) {...} $digest: function() { ... } }rootScope.js
function TodoCtrl($scope, $http) { $scope.todos = $http({method: 'GET', url: '/todos'}) // promises $scope.done = function(todo) { todo.status = 'done'; ...} }
function TodoCtrl($scope, $http) {... // all external deps
var http = function() { return [{name: "Task1"}] } var scope = {} TodoCtrl(http, scope) expect(scope.todos).toEqual([...])