Permalink
Browse files

progress

  • Loading branch information...
1 parent b0f4cfa commit f858f7efa059705e0418ae8461ca2491fcd1fc6f @jpka committed Dec 19, 2012
Showing with 66 additions and 76 deletions.
  1. +29 −48 backbone.sync.coffee
  2. +33 −3 backbone.sync.tests.coffee
  3. +1 −23 grunt.js
  4. +3 −2 package.json
View
77 backbone.sync.coffee
@@ -1,66 +1,47 @@
-methodTable =
- read: "findOne"
- create: "insert"
- update: "update"
- delete: "destroy"
-
Backbone.miniMongoSync = (method, model, options, error) ->
coll = model.mCollection || model.collection.mCollection
- # Backwards compatibility with Backbone <= 0.3.3
- if typeof options is "function"
- options =
- success: options
- error: error
-
- # if typeof coll is "string"
- # coll = new Meteor.Collection(coll)
- # if model.mCollection
- # model.mCollection = coll
- # else
- # model.collection.mCollection = coll
-
syncDfd = $? and $.Deferred and $.Deferred() #If $ is having Deferred - use it.
- #if Meteor.is_client
+ callback = (error, ret=true) ->
+ if error
+ options.error error
+ #model.trigger "error", model, null, options
+ syncDfd.resolve() if syncDfd
+ else
+ options.success ret
+ #model.trigger "sync", model, null, options
+ syncDfd.reject() if syncDfd
+
try
- coll[methodTable[method]] model.attributes, (error, id) ->
- if error
- options.error model, "Record not found"
- #model.trigger "error", model, null, options
- syncDfd.reject() if syncDfd
- else
- options.success
- id: id
- #model.trigger "sync", model, null, options
- syncDfd.resolve() if syncDfd
+ switch method
+ when "create"
+ coll.insert model.attributes, (error, id) ->
+ callback error, {id: id}
+ when "update"
+ coll.update {id: model.id}, {$set: model.attributes}, false, callback
+ when "read"
+ if model.attributes
+ ret = coll.findOne(model.attributes)
+ else
+ ret = coll.find().fetch()
+
+ error = null#if ret? not instanceof Array or ret.length > 0 then null else "Record not found"
+ callback error, ret
+ when "delete"
+ coll.remove {id: model.id}, callback
+
catch error
options.error error
-
- # else if Meteor.is_server
- # id = coll[methodTable[method]] model.attributes
- # options.success
- # id: id
-
- # switch method
- # when "read":
- # dbDo "findOne", model
- # when "create":
- # dbDo "insert", model
- # when "update":
- # dbDo "update", model
- # db.update model, cb
- # when "delete":
-
- # db.destroy model, cb
+ syncDfd.reject() if syncDfd
syncDfd && syncDfd.promise()
Backbone.ajaxSync = Backbone.sync
#Decide which sync method to use
Backbone.getSyncMethod = (model) ->
- if Meteor.is_server or model.mCollection or (model.collection and model.collection.mCollection)
+ if Meteor.is_server or model.mCollection or model.collection?.mCollection
Backbone.miniMongoSync
else
Backbone.ajaxSync
View
36 backbone.sync.tests.coffee
@@ -34,10 +34,11 @@ attrs =
author: "Bill Shakespeare"
length: 123
+mLibrary = new Meteor.Collection("library")
+
Library = Backbone.Collection.extend
- mCollection: "library"
+ mCollection: mLibrary
-mLibrary = new Meteor.Collection("library")
if Meteor.is_server
mLibrary.allow
insert: -> true
@@ -52,6 +53,7 @@ setup = ->
mLibrary.remove {}
tearDown = ->
+ mLibrary.remove {}
addTest = (desc, daTest) ->
Tinytest.add desc, (test) ->
@@ -66,7 +68,7 @@ addAsyncTest = (desc, daTest) ->
tearDown()
done()
-addAsyncTest "Backbone.miniMongoSync - Model saves", (test, done) ->
+addAsyncTest "Backbone.miniMongoSync - create", (test, done) ->
book = new Book()
book.save attrs,
success: ->
@@ -99,7 +101,35 @@ if Meteor.is_client
book.save({_id: "1"})
.fail ->
test.ok(true)
+
+ addAsyncTest "Backbone.miniMongoSync - update", (test, done) ->
+ book = new Book(attrs)
+ id = mLibrary.insert attrs
+ book.id = id
+
+ newAuthor = "William Shakespeare"
+ book.save {author: newAuthor},
+ success: ->
+ test.equal book.get("author"), newAuthor
+ console.log book.get("id")
+ test.equal mLibrary.findOne({_id: book.id}).author, newAuthor
done()
+ error: ->
+ test.fail()
+ done()
+
+# addAsyncTest "Backbone.miniMongoSync - fetch", (test, done) ->
+# mLibrary.insert attrs
+# library = new Library()
+
+# #debugger
+# library.fetch()
+# .done ->
+# test.equal library.models[0].attributes, mLibrary.findOne()
+# done()
+# .fail ->
+# test.fail()
+# done()
# Tinytest.add "Tinytest does not mantain state", (test) ->
# mLibrary.findOne attrs, (error, a) ->
View
24 grunt.js
@@ -3,28 +3,6 @@ module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
- // lint: {
- // files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
- // },
- // watch: {
- // files: '<config:lint.files>',
- // tasks: 'lint test'
- // },
- // jshint: {
- // options: {
- // curly: true,
- // eqeqeq: true,
- // immed: true,
- // latedef: true,
- // newcap: true,
- // noarg: true,
- // sub: true,
- // undef: true,
- // boss: true,
- // eqnull: true
- // },
- // globals: {}
- // }
coffee: {
src: {
files: {
@@ -50,7 +28,7 @@ module.exports = function(grunt) {
grunt.registerTask("default", "coffee");
grunt.registerTask("test", function() {
grunt.tasks("coffee");
- require("child_process").spawn("mrt", [], {stdio: "inherit"});
+ require("child_process").spawn("node_modules/.bin/mrt", [], {stdio: "inherit"});
});
grunt.registerTask("autotest", "test watch:all");
};
View
5 package.json
@@ -1,7 +1,8 @@
{
- "name": "Meteor smart package for backbone syncing",
+ "name": "meteor-backbone-sync",
"version": "0.0.1",
"devDependencies": {
- "grunt-contrib-coffee": "~0.3.2"
+ "grunt-contrib-coffee": "~0.3.2",
+ "meteorite": "~0.4.6"
}
}

0 comments on commit f858f7e

Please sign in to comment.