lib/volt/page/tasks.rb in volt-0.4.9 vs lib/volt/page/tasks.rb in volt-0.4.10

- old
+ new

@@ -40,34 +40,55 @@ when 'reload' reload end end - def response(callback_id, *args) + def response(callback_id, result, error) callback = @callbacks.delete(callback_id) if callback - callback.call(*args) + if error + # TODO: full error handling + puts "Error: #{error.inspect}" + else + callback.call(result) + end end end def changed(model_id, data) $loading_models = true - puts "UPDATE: #{model_id} with #{data.inspect}" - Store.update(model_id, data) + puts "From Backend: UPDATE: #{model_id} with #{data.inspect}" + Persistors::ModelStore.update(model_id, data) $loading_models = false end def added(path, data) $loading_models = true - puts "Add: #{path.inspect} - #{data.inspect}" - $page.store.send(path) << data + + # Don't add if already in there + # TODO: shouldn't send twice + unless Persistors::ModelStore.from_id(data[:_id]) + + _, parent_id = data.find {|k,v| k != :_id && k[-3..-1] == '_id'} + if parent_id + parent_collection = Persistors::ModelStore.from_id(parent_id).model + else + # On the root + parent_collection = $page.store + end + + puts "From Backend: Add: #{path.inspect} - #{data.inspect}" + parent_collection.send(path) << data + end $loading_models = false end def removed(id) + puts "From Backend: Remove: #{id}" $loading_models = true - Store.from_id(id).delete! + model = Persistors::ModelStore.from_id(id) + model.delete! $loading_models = false end def reload puts "RELOAD" \ No newline at end of file