Skip to content

Instantly share code, notes, and snippets.

@coderberry
Last active August 29, 2015 13:57
Show Gist options
  • Save coderberry/9440824 to your computer and use it in GitHub Desktop.
Save coderberry/9440824 to your computer and use it in GitHub Desktop.
import Search from 'appkit/libs/search';
module('Unit: lib/search', {
setup: function() {},
teardown: function() {}
});
test('ajaxData', function() {
var search = Search.create();
var ad = search.get('ajaxData');
equal(ad.tool_id, null);
equal(ad.query, null);
search.set('toolId', 'youtube');
search.set('searchText', 'education');
ad = search.get('ajaxData');
equal(ad.tool_id, 'youtube');
equal(ad.query, 'education');
});
test('performSearch should fetch and parse results', function() {
expect(2);
ic.ajax.defineFixture('/api/v1/search', {
response: [{name: 'basket weaving'}],
jqXHR: {},
textStatus: 'success'
});
var search = Search.create({ toolId: 'youtube', searchText: 'education' });
ok(search.get('isLoaded'));
stop();
search.performSearch().then(function(result) {
console.log("RESULT: ", result);
ok(result);
start();
});
});
import ajax from 'appkit/utils/ajax';
var Search = Ember.Object.extend({
isLoaded : true,
nextCriteria : null,
loadingMore : false,
toolId : null,
searchText : null,
searchResults : null,
init: function() {
this._super.apply(this, arguments);
if (!this.get('searchText')) {
this.set('searchText', '');
}
if (!this.get('searchResults')) {
this.set('searchResults', Em.A([]));
}
},
performSearch: function() {
var _this = this;
this.set('isLoaded', false);
this.get('searchResults').clear();
return this.performRequest();
// return this.performRequest().then(
// function(result) {
// Ember.run(function() {
// return _this.parseResults(result);
// });
// },
// function (err) {
// Ember.run(function() {
// return err;
// });
// }
// );
},
ajaxData: function () {
return {
tool_id: this.get('toolId'),
query: this.get('searchText')
};
}.property('toolId', 'searchText'),
performRequest: function() {
return ic.ajax.raw({
type: 'POST',
url: '/api/v1/search',
data: this.get('ajaxData')
});
},
parseResults: function(results) {
return results;
}
});
export default Search;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment