Skip to content

Instantly share code, notes, and snippets.

@erichocean
Created April 9, 2011 04:37
Show Gist options
  • Save erichocean/911133 to your computer and use it in GitHub Desktop.
Save erichocean/911133 to your computer and use it in GitHub Desktop.
Shows how to load tab-delimited objects properties (newline separated).
/*global Timesheet */
Timesheet.datasource = SC.DataSource.create({
fetch: function(store, query) {
if (query.fetch) return query.fetch(store);
else return NO;
}
});
Timesheet.store = SC.Store.create().from(Timesheet.datasource);
/*globals Timesheet */
Timesheet.Employee = SC.Record.extend({
firstName: SC.Record.attr(String, { key: 'first_name' }),
lastName: SC.Record.attr(String, { key: 'surname' }),
fullName: function() {
return this.getEach('firstName', 'lastName').compact().join(' ');
}.property('firstName', 'lastName').cacheable(),
icon: 'sc-icon-user-16'
});
// TODO: Make this async() and move code to the query.
var response = SC.Request.getUrl(sc_static('employees.txt')).async(NO).send();
var start = new Date().getTime();
var rows = response.get('body').split('\n');
var finish = new Date().getTime();
console.log("Loaded " + rows.length + " employees in "+ (finish-start).toString() + " ms.");
Timesheet.set('employeeRows', rows);
Timesheet.ALL_EMPLOYEES = SC.Query.remote(Timesheet.JobCostCode, {
fetch: function(store) {
// console.log("fetch()");
var ary = SC.SparseArray.create({
delegate: this,
store: store,
query: this,
rows: Timesheet.employeeRows
});
ary.provideLength(ary.rows.length); // might as well do it now...
store.loadQueryResults(this, ary);
return YES;
},
sparseArrayDidRequestLength: function(sparseArray) {
// console.log("sparseArrayDidRequestLength()");
sparseArray.provideLength(sparseArray.rows.length);
},
sparseArrayDidRequestRange: function(sparseArray, range) {
// console.log("sparseArrayDidRequestRange()");
var storeKey, storeKeys = [], store = sparseArray.get('store'),
idx, len, id, ary, rows = sparseArray.rows, K = SC.Record,
recordType = Timesheet.Employee;
// load the requested records into the store
for (idx=range.start, len=range.start+range.length; idx<len; ++idx) {
ary = rows[idx].split('\t');
id = ary[0] + '-' + ary[2]; // needs a composite key
storeKey = recordType.storeKeyFor(id);
storeKeys.push(storeKey);
// don't need to create a hash twice...
if (store.peekStatus(storeKey) === K.EMPTY) {
store.pushRetrieve(recordType, id, {
first_name: ary[1],
middle_Name: ary[2],
surname: ary[3] // FIXME: Do the remaining keys.
}, storeKey);
}
}
// provide the storeKeys for those records to the sparse array
sparseArray.provideObjectsInRange({ start: range.start, length: range.length }, storeKeys);
sparseArray.rangeRequestCompleted(range.start);
}
});
get '/api/v1/employees' do
ActiveRecord::Base.establish_connection( config )
connection = ActiveRecord::Base.connection
sql = "SELECT RTRIM(id) as id, first_name, middle_name, surname, RTRIM(pay_class) AS pay_class, occupation, RTRIM(employment_type) as employment_type, subcontractor, department FROM EMPLOYEES ORDER BY id"
response = []
results = connection.execute(sql)
results.each do |r|
response << [r['id'], r['first_name'], r['middle_name'], r['surname'], r['pay_class'], r['occupation'], r['employment_type'], r['subcontractor'], r['department']].join("\t")
end
ActiveRecord::Base.clear_active_connections!
response = response.join("\n")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment