Skip to content

Instantly share code, notes, and snippets.

@adambankin
Created September 6, 2016 21:06
Show Gist options
  • Save adambankin/bca12c134083599464a3299cd5aeb19f to your computer and use it in GitHub Desktop.
Save adambankin/bca12c134083599464a3299cd5aeb19f to your computer and use it in GitHub Desktop.
/* use the View to control its associated Controller */
// file:: MyView1.js
var MyView1 = T1View.extend({
processResults: function (results) {
// yadda, yadda
renderResults();
},
// do everything for the Collection in the View
onSearch: function () {
var self = this;
var collection = this.collection;
var resolvedData;
collection.fetchOptions = {
some: 'values',
you: 'want',
sent: 'to',
the: 'server',
// ...
q: this.el.find('input[text]').val().trim()
};
collection.fetch({
success: function (results) {
self.processResults(results);
}
});
}
});
// file:: MyCollection1.js
var myCollection1 = T1Collection.extend({
// default T1Collection methods
});
/* Use the View to collect values for the Collection, but run everything in the Collection
* Just use the View for rendering content
*/
// file:: MyView2.js
var MyView2 = T1View.extend({
renderResults: function (results) {
// yadda, yadda
},
onSearch: function () {
var deferred = $.Deferred();
var searchTerm = this.el.find('input[text]').val().trim();
// do all the work in the Collection
return this.collection.doStuffInCollection(searchTerm)
.then(this.renderResults.bind(this));
}
});
// file:: MyCollection2.js
var myCollection2 = T1Collection.extend({
processResults: function (results) {
// yadda, yadda
return results;
},
doStuffInCollection: function (searchTerm, success) {
this.fetchOptions = {
some: 'values',
you: 'want',
sent: 'to',
the: 'server',
// ...
q: searchTerm
};
// this is kind of pseudo code because jQuery's .then() doesn't chain this way
// basically it's about returning the promise from .fetch() to the View
return this.fetch().then(this.renderResults.bind(this));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment