This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grunt.initConfig({ | |
less: { | |
src: ['styles.less'], | |
dest: 'styles.css' | |
}, | |
postcss: { | |
options: { | |
processors: [ | |
require('autoprefixer')(), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
css: | |
lessc style.less | autoprefixer-cli > style.css |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Router = Backbone.Router.extend({ | |
before: function() { | |
console.log('before'); | |
}, | |
after: function() { | |
console.log('after'); | |
}, | |
routes: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// save a reference to route() method | |
var $route = Backbone.Router.prototype.route; | |
// then override it | |
Backbone.Router.prototype.before = function() {} | |
Backbone.Router.prototype.after = function() {} | |
Backbone.Router.prototype.route = function(route, name, callback) { | |
if (_.isFunction(name)) { | |
callback = name; | |
name = ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var coll = new Backbone.Collection([model1, model2]); | |
console.log(coll.search('Jordan').first().get('last_name')); // Aslam | |
console.log(coll.search('John').first().get('last_name')); // Doe | |
console.log(coll.search(/Jo/).pluck('last_name')); // ['Aslam', 'Doe'] | |
console.log(coll.search(/Jordan|John/).pluck('last_name')): // ['Aslam', 'Doe'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.Collection.prototype.search = function(test) { | |
return this.filter(function(model) { | |
return model.match(test); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var model1 = new Backbone.Model({ first_name: 'Jordan', last_name: 'Aslam' }); | |
var model2 = new Backbone.Model({ first_name: 'John', last_name: 'Doe' }); | |
console.log(model1.match(‘Jordan’)); // true | |
console.log(model2.match(‘Doe’)); // true | |
console.log(model1.match(/Jo/)); // true | |
console.log(model2.match(/Jo/)); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.Model.prototype.match = function(test) { | |
return _.any(this.attributes, function(attr) { | |
return _.isRegExp(test) ? test.test(attr) : attr == test; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Backbone.View.extend({ | |
initialize: function() { | |
// obviously .foo element do not yet exists because view is not rendered | |
// this line will throw an error | |
this.$(‘.foo’).text(‘foo-bar-baz’); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// save a reference to $() method | |
var view$ = Backbone.View.prototype.$; | |
// then override it | |
Backbone.View.prototype.$ = function(selector) { | |
var element = view$.apply(this, arguments); | |
if (!element.length === 0) { | |
console.error("[Backbone.View] Warning: selector '" + selector + "' do not match any element"); | |
} |