Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

replaced localstorage by sync with django-rest-framework API

  • Loading branch information...
commit cc627c8a549fbe4a58906bfecf7b514adb7dc064 1 parent 7d909cd
@sebastibe authored
View
5 todos-example/frontend/app/collections/todo_list.coffee
@@ -2,11 +2,8 @@
class exports.TodoList extends Backbone.Collection
+ url: '/api/todos/'
model: Todo
- url: '/api/todos'
-
- initialize: ->
- @localStorage = new Store 'todos'
done: ->
@filter (todo) ->
View
5 todos-example/frontend/app/initialize.coffee
@@ -11,6 +11,11 @@ class exports.Application extends BrunchApplication
# This callback would be executed on document ready event.
initialize: ->
@todoList = new TodoList
+ $.when(@todoList.fetch()).done(->
+ console.debug "todo list fetched"
+ ).fail(->
+ console.error "todo list failed to fetch"
+ )
@routers.main = new MainRouter
@views.home = new HomeView
@views.newTodo = new NewTodoView
View
1  todos-example/frontend/app/models/todo.coffee
@@ -1,4 +1,5 @@
class exports.Todo extends Backbone.Model
+ idAttribute: 'pk'
defaults:
content: 'Empty todo...'
View
104 todos-example/frontend/vendor/scripts/backbone.localStorage.js
@@ -1,104 +0,0 @@
-/**
- * Backbone localStorage Adapter v1.0
- * https://github.com/jeromegn/Backbone.localStorage
- */
-
-// A simple module to replace `Backbone.sync` with *localStorage*-based
-// persistence. Models are given GUIDS, and saved into a JSON object. Simple
-// as that.
-
-// Generate four random hex digits.
-function S4() {
- return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
-};
-
-// Generate a pseudo-GUID by concatenating random hexadecimal.
-function guid() {
- return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
-};
-
-// Our Store is represented by a single JS object in *localStorage*. Create it
-// with a meaningful name, like the name you'd give a table.
-window.Store = function(name) {
- this.name = name;
- var store = localStorage.getItem(this.name);
- this.records = (store && store.split(",")) || [];
-};
-
-_.extend(Store.prototype, {
-
- // Save the current state of the **Store** to *localStorage*.
- save: function() {
- localStorage.setItem(this.name, this.records.join(","));
- },
-
- // Add a model, giving it a (hopefully)-unique GUID, if it doesn't already
- // have an id of it's own.
- create: function(model) {
- if (!model.id) model.id = model.attributes.id = guid();
- localStorage.setItem(this.name+"-"+model.id, JSON.stringify(model));
- this.records.push(model.id.toString());
- this.save();
- return model;
- },
-
- // Update a model by replacing its copy in `this.data`.
- update: function(model) {
- localStorage.setItem(this.name+"-"+model.id, JSON.stringify(model));
- if (!_.include(this.records, model.id.toString())) this.records.push(model.id.toString()); this.save();
- return model;
- },
-
- // Retrieve a model from `this.data` by id.
- find: function(model) {
- return JSON.parse(localStorage.getItem(this.name+"-"+model.id));
- },
-
- // Return the array of all models currently in storage.
- findAll: function() {
- return _.map(this.records, function(id){return JSON.parse(localStorage.getItem(this.name+"-"+id));}, this);
- },
-
- // Delete a model from `this.data`, returning it.
- destroy: function(model) {
- localStorage.removeItem(this.name+"-"+model.id);
- this.records = _.reject(this.records, function(record_id){return record_id == model.id.toString();});
- this.save();
- return model;
- }
-
-});
-
-// localSync delegate to the model or collection's
-// *localStorage* property, which should be an instance of `Store`.
-Backbone.localSync = function(method, model, options, error) {
-
- // Backwards compatibility with Backbone <= 0.3.3
- if (typeof options == 'function') {
- options = {
- success: options,
- error: error
- };
- }
-
- var resp;
- var store = model.localStorage || model.collection.localStorage;
-
- switch (method) {
- case "read": resp = model.id != undefined ? store.find(model) : store.findAll(); break;
- case "create": resp = store.create(model); break;
- case "update": resp = store.update(model); break;
- case "delete": resp = store.destroy(model); break;
- }
-
- if (resp) {
- options.success(resp);
- } else {
- options.error("Record not found");
- }
-};
-
-// Override 'Backbone.sync' to default to localSync,
-// the original 'Backbone.sync' is still available in 'Backbone.ajaxSync'
-Backbone.ajaxSync = Backbone.sync;
-Backbone.sync = Backbone.localSync;
View
1  todos-example/todos/models.py
@@ -4,3 +4,4 @@
class Todo(models.Model):
content = models.CharField(max_length=200)
done = models.BooleanField(default=False)
+ order = models.IntegerField()
View
1  todos-example/todos/resources.py
@@ -5,3 +5,4 @@
class TodoResource(ModelResource):
model = Todo
+ fields = ('content', 'done', 'order', 'pk')
Please sign in to comment.
Something went wrong with that request. Please try again.