17.03.2014, codeshow
// Backbone
var Person = Backbone.Model.extend();
var person = new Person();
person.set({name: 'Nick'});
console.log(person.get('name'));
// AngularJS
$scope.person = new Person();
$scope.person.name = 'Nick';
console.log(person.name);
// LearningUnit_test.js
beforeEach(module(function($provide) {
$provide.value('berryService', jasmine.createSpyObj('berryService', ['getBerries']));
}));
it('checks that user got 5 berries for completing unit', function() {
var lu = new LearningUnit();
lu.completeUnit();
// check that user got 5 berries for completing unit
expect(berryService.getBerries).toHaveBeenCalledWith(5);
});
// LearningUnit.js
app.service("LearningUnit", function(berryService, User, lrsService, ...) {
this.completeUnit = function() { berryService.getBerries(5); };
}
angular.module("template/accordion/accordion-group.html", [])
.run(["$templateCache", function($templateCache) {
$templateCache.put("template/accordion/accordion-group.html",
"\n" +
" \n" +
" \n" +
" \n" +
"");
}]);
app.factory("LearningUnit", function($http, $q, $rootScope, courseService, statsService,
berryService, lrsService, backend, util, User, lang, myvocab) {
// dependencies injected based on arguments names - AngularJS internally
});
// after minification
app.factory("LearningUnit", function(a, b, c, d...) {
// dependencies can't be injected based on arguments
});
// after annotation
app.factory("LearningUnit", ['$http', '$q', '$rootScope', 'courseService', 'berryService',
'lrsService', 'backend', 'util', 'User', 'lang', 'myvocab',
function($http, $q, $rootScope, courseService, statsService, berryService,
lrsService, backend, util, User, lang, myvocab) {
// body
}]);
...
$ grunt sails-linker
// Backbone
person.set({name: 'Filip'});
// model knows on every set invoke that there was a change to person.name
// AngularJS
$scope.$apply(function() {
person.name = 'Filip';
});
// AngularJS needs to compare 'person.name' (and all other model variables) to last known value to detect change
// HTML interpolated, naive approach
{{ translate('Congratulations. You finished a grammar unit.') }}
// HTML directive
Congratulations. You finished a grammar unit.
// JavaScript
app.directive('trs', function(translate) {
return {
link: function(scope, elm) {
var translated = translate(elm.text());
elm.replaceWith(translated);
}
}
}
// HTML interpolated
{{ translate('Congratulations. You finished a grammar unit.') }}
// HTML directive
Congratulations. You finished a grammar unit.
// JavaScript
app.directive('trs', function(translate) {
return {
link: function(scope, elm) {
var sourceText = elm.text();
function fill() {
var translated = translate(sourceText);
elm.html(translated);
}
scope.$on('languageChanged', fill);
fill();
}
}
}
app.directive('removeWatchers', function($timeout) {
return {
scope: false,
link: function(scope, elm, attrs) {
function removeWatchers(scope) {
// remove watchers from scope.$$watchers and all scope children
...
}
$timeout(function() {
// this will execute after first digest cycle
removeWatchers();
});
}
};
});
{{ unit.title }}
// not a good idea!
$(body).onScroll(function() {
scope.$apply(function() {
updateUI();
});
});
// better, executed at most once every 300msec
var throttled = _.throttle(function() {
scope.$apply(function() {
updateUI();
});
}, 300);
var beingWatched = {};
// Define callback function to get notified on changes
function somethingChanged(changes) {
// do something
}
Object.observe(beingWatched, somethingChanged);