Permalink
Browse files

Progress.

  • Loading branch information...
1 parent 7da77f5 commit b0f4cfa9b8ddf131c03f8e31bbfadfc3fbd0158f @jpka committed Dec 14, 2012
Showing with 219 additions and 37 deletions.
  1. +2 −0 .gitignore
  2. +38 −15 backbone.sync.coffee
  3. +107 −17 backbone.sync.tests.coffee
  4. +56 −0 grunt.js
  5. +4 −3 package.js
  6. +7 −0 package.json
  7. +5 −2 smart.json
View
@@ -1,2 +1,4 @@
.meteor/
smart.lock
+node_modules/
+build/
View
@@ -5,23 +5,42 @@ methodTable =
delete: "destroy"
Backbone.miniMongoSync = (method, model, options, error) ->
- db = model.db || model.collection.db
+ coll = model.mCollection || model.collection.mCollection
# Backwards compatibility with Backbone <= 0.3.3
- if typeof options == "function"
+ if typeof options is "function"
options =
success: options
error: error
- syncDfd = $.Deferred && $.Deferred(); #If $ is having Deferred - use it.
+ # if typeof coll is "string"
+ # coll = new Meteor.Collection(coll)
+ # if model.mCollection
+ # model.mCollection = coll
+ # else
+ # model.collection.mCollection = coll
- db[methodTable[method]] model.attributes, (error) ->
- if error
- options.error "Record not found"
- syncDfd.reject() if syncDfd
- else
- options.success model
- syncDfd.resolve() if syncDfd
+ syncDfd = $? and $.Deferred and $.Deferred() #If $ is having Deferred - use it.
+
+ #if Meteor.is_client
+ 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
+ catch error
+ options.error error
+
+ # else if Meteor.is_server
+ # id = coll[methodTable[method]] model.attributes
+ # options.success
+ # id: id
# switch method
# when "read":
@@ -39,10 +58,14 @@ Backbone.miniMongoSync = (method, model, options, error) ->
Backbone.ajaxSync = Backbone.sync
-# Override 'Backbone.sync' to detect if it is a minimongo model/collection or not
-# and decide a sync method accordingly
-Backbone.sync = (method, model, options, error) ->
- if model.db or (model.collection and model.collection.db)
+#Decide which sync method to use
+Backbone.getSyncMethod = (model) ->
+ if Meteor.is_server or model.mCollection or (model.collection and model.collection.mCollection)
Backbone.miniMongoSync
else
- Backbone.ajaxSync
+ Backbone.ajaxSync
+
+# Override 'Backbone.sync' to detect if it is a minimongo model/collection or not
+# and use the appropiate sync method
+Backbone.sync = (method, model, options, error) ->
+ Backbone.getSyncMethod(model)(method, model, options, error)
@@ -1,26 +1,116 @@
-Tinytest.add "Backbone.sync - defaults to ajax", (test) ->
- m = new Backbone.Model()
+if Meteor.is_server
+ Tinytest.add "Backbone.sync - uses only mm", (test) ->
+ m = new Backbone.Model()
- #remoteModel should use ajax sync
- test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync
+ #remoteModel should use ajax sync
+ test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
- #Backbone.sync should return a value when ajax is used."
- test.notEqual m.fetch({url: "/"}), undefined
+ M = Backbone.Model.extend
+ mCollection: "collection"
+ m = new M()
-# module("Backbone.sync", _.extend(new Environment, {
+ test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
-# setup : function() {
-# Environment.prototype.setup.apply(this, arguments);
-# library = new Library;
-# library.create(attrs, {wait: false});
-# },
+if Meteor.is_client
+ Tinytest.add "Backbone.sync - defaults to ajax", (test) ->
+ m = new Backbone.Model()
-# teardown: function() {
-# Environment.prototype.teardown.apply(this, arguments);
-# Backbone.emulateHTTP = false;
-# }
+ #remoteModel should use ajax sync
+ test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync
-# }));
+ #Backbone.sync should return a value when ajax is used."
+ test.notEqual m.fetch({url: "/"}), undefined
+
+if Meteor.is_client
+ Tinytest.add "Backbone.sync - uses minimongo when a meteor collection is specified", (test) ->
+ M = Backbone.Model.extend
+ mCollection: "collection"
+ m = new M()
+
+ test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
+
+attrs =
+ title: "The Tempest"
+ author: "Bill Shakespeare"
+ length: 123
+
+Library = Backbone.Collection.extend
+ mCollection: "library"
+
+mLibrary = new Meteor.Collection("library")
+if Meteor.is_server
+ mLibrary.allow
+ insert: -> true
+ remove: -> true
+ update: -> true
+
+Book = Backbone.Model.extend
+ idAttribute: "id"
+ mCollection: mLibrary
+
+setup = ->
+ mLibrary.remove {}
+
+tearDown = ->
+
+addTest = (desc, daTest) ->
+ Tinytest.add desc, (test) ->
+ setup()
+ daTest(test)
+ tearDown()
+
+addAsyncTest = (desc, daTest) ->
+ Tinytest.addAsync desc, (test, done) ->
+ setup()
+ daTest test, ->
+ tearDown()
+ done()
+
+addAsyncTest "Backbone.miniMongoSync - Model saves", (test, done) ->
+ book = new Book()
+ book.save attrs,
+ success: ->
+ rec = mLibrary.findOne(attrs)
+ test.equal (typeof rec._id), "string"
+ test.equal book.id, rec._id
+ done()
+ error: ->
+ test.fail()
+ done()
+
+addAsyncTest "Backbone.miniMongoSync - invalid model fails to save", (test, done) ->
+ book = new Book()
+ book.save {_id: "1"},
+ success: ->
+ test.fail()
+ done()
+ error: (model, error) ->
+ test.notEqual error, undefined
+ done()
+
+if Meteor.is_client
+ addAsyncTest "Backbone.miniMongoSync - save returns a promise", (test, done) ->
+ book = new Book()
+ book.save(attrs)
+ .done ->
+ test.ok(true)
+ done()
+
+ book.save({_id: "1"})
+ .fail ->
+ test.ok(true)
+ done()
+
+# Tinytest.add "Tinytest does not mantain state", (test) ->
+# mLibrary.findOne attrs, (error, a) ->
+# console.log error
+# test.equal typeof id, "string"
+
+ # book = new Book(attrs)
+ # book.save
+ # success: ->
+ # mLibrary.findOne attrs, (error, id) ->
+ # test.equal id, book.id
# test("read", 4, function() {
# library.fetch();
View
@@ -0,0 +1,56 @@
+/*global module:false*/
+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: {
+ "build/backbone.sync.js": "backbone.sync.coffee"
+ }
+ },
+ tests: {
+ files: {
+ "build/backbone.sync.tests.js": "backbone.sync.tests.coffee"
+ }
+ }
+ },
+ watch: {
+ all: {
+ files: "*.coffee",
+ tasks: "coffee"
+ }
+ }
+ });
+
+ grunt.loadNpmTasks("grunt-contrib-coffee");
+
+ grunt.registerTask("default", "coffee");
+ grunt.registerTask("test", function() {
+ grunt.tasks("coffee");
+ require("child_process").spawn("mrt", [], {stdio: "inherit"});
+ });
+ grunt.registerTask("autotest", "test watch:all");
+};
View
@@ -7,11 +7,12 @@ Package.on_use(function (api, where) {
//api.use(["jquery", "json"]);
where = where || ['client', 'server'];
- api.use('backbone', where);
+ api.use('backbone', ['client', 'server']);
+ api.add_files('build/backbone.sync.js', ['client', 'server']);
});
Package.on_test(function (api) {
- api.use(['minimongo', 'backbone'], 'client');
+ api.use(['minimongo', 'backbone'], ['client', 'server']);
api.use('tinytest');
- api.add_files('backbone.sync.tests.js', 'client');
+ api.add_files('build/backbone.sync.tests.js', ['client', 'server']);
});
View
@@ -0,0 +1,7 @@
+{
+ "name": "Meteor smart package for backbone syncing",
+ "version": "0.0.1",
+ "devDependencies": {
+ "grunt-contrib-coffee": "~0.3.2"
+ }
+}
View
@@ -4,5 +4,8 @@
"homepage": "https://github.com/jpka/meteor-backbone-sync",
"author": "Juan Pablo Kaniefsky",
"version": "0.0.1",
- "git": "https://github.com/jpka/meteor-backbone-sync.git"
-}
+ "git": "https://github.com/jpka/meteor-backbone-sync.git",
+ "meteor": {
+ "branch": "v0.5.2"
+ }
+}

0 comments on commit b0f4cfa

Please sign in to comment.