|
// Rivets adapter for Stapes |
|
rivets.adapters[':'] = { |
|
subscribe: function(obj, keypath, callback) { |
|
obj.on("change:" + keypath, callback) |
|
}, |
|
|
|
unsubscribe: function(obj, keypath, callback) { |
|
obj.off("change:" + keypath, callback) |
|
}, |
|
|
|
read: function(obj, keypath) { |
|
return obj.get(keypath) |
|
}, |
|
|
|
publish: function(obj, keypath, value) { |
|
obj.set(keypath, value) |
|
} |
|
} |
|
|
|
|
|
// sample data |
|
var lawyers = [ |
|
{"name":"Tarik Y. Guthrie", "areas":['area one', 'area two']}, |
|
{"name":"Allen C. Lamb", "areas":['area three']}, |
|
{"name":"Venus I. Wheeler", "areas":['area two']}, |
|
{"name":"Brenda A. Coffey", "areas": ['area one']}, |
|
{"name":"Rana A. Salas", "areas":['area three']}] |
|
|
|
|
|
// Lawyers module |
|
var Lawyers = Stapes.subclass({ |
|
constructor: function (list) { |
|
// initialize list of lawyers |
|
this.list = list || [] |
|
console.log('new Lawyers object', this, this.all) |
|
}, |
|
|
|
// return the full list of lawyers |
|
all: function() { |
|
return this.list |
|
}, |
|
|
|
// return a filtered list of lawyers based on passed in attributes |
|
filter: function (name, area) { |
|
console.log('Lawyers.filter', name, area) |
|
return _.filter(this.all(), function (item) { |
|
return (item.name.toLowerCase().indexOf(name.toLowerCase()) > -1) && (_.contains(_.toArray(item.areas), area) || !area) |
|
}) |
|
} |
|
}) |
|
|
|
|
|
// Lawyers controller |
|
var Controller = Stapes.subclass({ |
|
constructor: function (model) { |
|
// make 'this' work sanely |
|
_.bindAll(this) |
|
|
|
// store reference to Lawyers Module |
|
this.Lawyers = model |
|
|
|
// set up properties used for user search and filter |
|
this.set('search', '') |
|
this.set('area', '') |
|
this.set('lawyers', this.Lawyers.all()) |
|
this.filter() |
|
}, |
|
|
|
// the : adapter only works on change...this triggers the change event on keyup. |
|
update: function (event, binding) { |
|
event.target.dispatchEvent(new Event('change')); |
|
this.filter() |
|
}, |
|
|
|
// update our local list of lawyers based on user filters |
|
filter: function () { |
|
this.set('lawyers', this.Lawyers.filter(this.get('search'), this.get('area'))) |
|
this.set('found', this.get('lawyers').length) |
|
} |
|
|
|
}) |
|
|
|
// should probably be in DOM-ready |
|
// instantiate the controller and Lawyers module, passing in our array of lawyers |
|
// lawyers could easily be loaded via AJAX |
|
var app = new Controller(new Lawyers(lawyers)) |
|
|
|
// bind dom to controller |
|
var view = rivets.bind(document.getElementById('app'), { app: app }) |