Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Progress.

  • Loading branch information...
commit b0f4cfa9b8ddf131c03f8e31bbfadfc3fbd0158f 1 parent 7da77f5
Juan Pablo Kaniefsky authored
2  .gitignore
... ...
@@ -1,2 +1,4 @@
1 1
 .meteor/
2 2
 smart.lock
  3
+node_modules/
  4
+build/
53  backbone.sync.coffee
@@ -5,23 +5,42 @@ methodTable =
5 5
   delete: "destroy"
6 6
 
7 7
 Backbone.miniMongoSync = (method, model, options, error) ->
8  
-  db = model.db || model.collection.db
  8
+  coll = model.mCollection || model.collection.mCollection
9 9
 
10 10
   # Backwards compatibility with Backbone <= 0.3.3
11  
-  if typeof options == "function"
  11
+  if typeof options is "function"
12 12
     options =
13 13
       success: options
14 14
       error: error
15 15
 
16  
-  syncDfd = $.Deferred && $.Deferred(); #If $ is having Deferred - use it.
  16
+  # if typeof coll is "string"
  17
+  #   coll = new Meteor.Collection(coll)
  18
+  #   if model.mCollection
  19
+  #     model.mCollection = coll
  20
+  #   else
  21
+  #     model.collection.mCollection = coll
17 22
 
18  
-  db[methodTable[method]] model.attributes, (error) ->
19  
-    if error
20  
-      options.error "Record not found"
21  
-      syncDfd.reject() if syncDfd
22  
-    else
23  
-      options.success model
24  
-      syncDfd.resolve() if syncDfd
  23
+  syncDfd = $? and $.Deferred and $.Deferred() #If $ is having Deferred - use it.
  24
+
  25
+  #if Meteor.is_client
  26
+  try
  27
+    coll[methodTable[method]] model.attributes, (error, id) ->
  28
+      if error
  29
+        options.error model, "Record not found"
  30
+        #model.trigger "error", model, null, options
  31
+        syncDfd.reject() if syncDfd
  32
+      else
  33
+        options.success
  34
+          id: id
  35
+        #model.trigger "sync", model, null, options
  36
+        syncDfd.resolve() if syncDfd
  37
+  catch error
  38
+    options.error error
  39
+
  40
+  # else if Meteor.is_server
  41
+  #   id = coll[methodTable[method]] model.attributes
  42
+  #   options.success
  43
+  #     id: id
25 44
 
26 45
   # switch method
27 46
   #   when "read":
@@ -39,10 +58,14 @@ Backbone.miniMongoSync = (method, model, options, error) ->
39 58
 
40 59
 Backbone.ajaxSync = Backbone.sync
41 60
 
42  
-# Override 'Backbone.sync' to detect if it is a minimongo model/collection or not
43  
-# and decide a sync method accordingly
44  
-Backbone.sync = (method, model, options, error) ->
45  
-  if model.db or (model.collection and model.collection.db)
  61
+#Decide which sync method to use
  62
+Backbone.getSyncMethod = (model) ->
  63
+  if Meteor.is_server or model.mCollection or (model.collection and model.collection.mCollection)
46 64
     Backbone.miniMongoSync
47 65
   else
48  
-    Backbone.ajaxSync
  66
+    Backbone.ajaxSync
  67
+
  68
+# Override 'Backbone.sync' to detect if it is a minimongo model/collection or not
  69
+# and use the appropiate sync method
  70
+Backbone.sync = (method, model, options, error) ->
  71
+  Backbone.getSyncMethod(model)(method, model, options, error)
124  backbone.sync.tests.coffee
... ...
@@ -1,26 +1,116 @@
1  
-Tinytest.add "Backbone.sync - defaults to ajax", (test) ->
2  
-  m = new Backbone.Model()
  1
+if Meteor.is_server
  2
+  Tinytest.add "Backbone.sync - uses only mm", (test) ->
  3
+    m = new Backbone.Model()
3 4
 
4  
-  #remoteModel should use ajax sync
5  
-  test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync
  5
+    #remoteModel should use ajax sync
  6
+    test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
6 7
 
7  
-  #Backbone.sync should return a value when ajax is used."
8  
-  test.notEqual m.fetch({url: "/"}), undefined
  8
+    M = Backbone.Model.extend
  9
+      mCollection: "collection"
  10
+    m = new M()
9 11
 
10  
-# module("Backbone.sync", _.extend(new Environment, {
  12
+    test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
11 13
 
12  
-#     setup : function() {
13  
-#       Environment.prototype.setup.apply(this, arguments);
14  
-#       library = new Library;
15  
-#       library.create(attrs, {wait: false});
16  
-#     },
  14
+if Meteor.is_client
  15
+  Tinytest.add "Backbone.sync - defaults to ajax", (test) ->
  16
+    m = new Backbone.Model()
17 17
 
18  
-#     teardown: function() {
19  
-#       Environment.prototype.teardown.apply(this, arguments);
20  
-#       Backbone.emulateHTTP = false;
21  
-#     }
  18
+    #remoteModel should use ajax sync
  19
+    test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync
22 20
 
23  
-#   }));
  21
+    #Backbone.sync should return a value when ajax is used."
  22
+    test.notEqual m.fetch({url: "/"}), undefined
  23
+
  24
+if Meteor.is_client
  25
+  Tinytest.add "Backbone.sync - uses minimongo when a meteor collection is specified", (test) ->
  26
+    M = Backbone.Model.extend
  27
+      mCollection: "collection"
  28
+    m = new M()
  29
+
  30
+    test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync
  31
+
  32
+attrs =
  33
+  title: "The Tempest"
  34
+  author: "Bill Shakespeare"
  35
+  length: 123
  36
+
  37
+Library = Backbone.Collection.extend
  38
+  mCollection: "library"
  39
+
  40
+mLibrary = new Meteor.Collection("library")
  41
+if Meteor.is_server
  42
+  mLibrary.allow
  43
+    insert: -> true
  44
+    remove: -> true
  45
+    update: -> true
  46
+
  47
+Book = Backbone.Model.extend
  48
+  idAttribute: "id"
  49
+  mCollection: mLibrary
  50
+
  51
+setup = ->
  52
+  mLibrary.remove {}
  53
+
  54
+tearDown = ->
  55
+
  56
+addTest = (desc, daTest) ->
  57
+  Tinytest.add desc, (test) ->
  58
+    setup()
  59
+    daTest(test)
  60
+    tearDown()
  61
+
  62
+addAsyncTest = (desc, daTest) ->
  63
+  Tinytest.addAsync desc, (test, done) ->
  64
+    setup()
  65
+    daTest test, ->
  66
+      tearDown()
  67
+      done()
  68
+
  69
+addAsyncTest "Backbone.miniMongoSync - Model saves", (test, done) ->
  70
+  book = new Book()
  71
+  book.save attrs,
  72
+    success: ->
  73
+      rec = mLibrary.findOne(attrs)
  74
+      test.equal (typeof rec._id), "string"
  75
+      test.equal book.id, rec._id
  76
+      done()
  77
+    error: ->
  78
+      test.fail()
  79
+      done()
  80
+
  81
+addAsyncTest "Backbone.miniMongoSync - invalid model fails to save", (test, done) ->
  82
+  book = new Book()
  83
+  book.save {_id: "1"},
  84
+    success: ->
  85
+      test.fail()
  86
+      done()
  87
+    error: (model, error) ->
  88
+      test.notEqual error, undefined
  89
+      done()
  90
+
  91
+if Meteor.is_client
  92
+  addAsyncTest "Backbone.miniMongoSync - save returns a promise", (test, done) ->
  93
+    book = new Book()
  94
+    book.save(attrs)
  95
+      .done ->
  96
+        test.ok(true)
  97
+        done()
  98
+
  99
+    book.save({_id: "1"})
  100
+      .fail ->
  101
+        test.ok(true)
  102
+        done()
  103
+
  104
+# Tinytest.add "Tinytest does not mantain state", (test) ->
  105
+#   mLibrary.findOne attrs, (error, a) ->
  106
+#     console.log error
  107
+#     test.equal typeof id, "string"
  108
+
  109
+  # book = new Book(attrs)
  110
+  # book.save
  111
+  #   success: ->
  112
+  #     mLibrary.findOne attrs, (error, id) ->
  113
+  #       test.equal id, book.id
24 114
 
25 115
 #   test("read", 4, function() {
26 116
 #     library.fetch();
56  grunt.js
... ...
@@ -0,0 +1,56 @@
  1
+/*global module:false*/
  2
+module.exports = function(grunt) {
  3
+
  4
+  // Project configuration.
  5
+  grunt.initConfig({
  6
+    // lint: {
  7
+    //   files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
  8
+    // },
  9
+    // watch: {
  10
+    //   files: '<config:lint.files>',
  11
+    //   tasks: 'lint test'
  12
+    // },
  13
+    // jshint: {
  14
+    //   options: {
  15
+    //     curly: true,
  16
+    //     eqeqeq: true,
  17
+    //     immed: true,
  18
+    //     latedef: true,
  19
+    //     newcap: true,
  20
+    //     noarg: true,
  21
+    //     sub: true,
  22
+    //     undef: true,
  23
+    //     boss: true,
  24
+    //     eqnull: true
  25
+    //   },
  26
+    //   globals: {}
  27
+    // }
  28
+    coffee: {
  29
+      src: {
  30
+        files: {
  31
+          "build/backbone.sync.js": "backbone.sync.coffee"
  32
+        }
  33
+      },
  34
+      tests: {
  35
+        files: {
  36
+          "build/backbone.sync.tests.js": "backbone.sync.tests.coffee"
  37
+        }
  38
+      }
  39
+    },
  40
+    watch: {
  41
+      all: {
  42
+        files: "*.coffee",
  43
+        tasks: "coffee"
  44
+      }
  45
+    }
  46
+  });
  47
+
  48
+  grunt.loadNpmTasks("grunt-contrib-coffee");
  49
+
  50
+  grunt.registerTask("default", "coffee");
  51
+  grunt.registerTask("test", function() {
  52
+    grunt.tasks("coffee");
  53
+    require("child_process").spawn("mrt", [], {stdio: "inherit"});
  54
+  });
  55
+  grunt.registerTask("autotest", "test watch:all");
  56
+};
7  package.js
@@ -7,11 +7,12 @@ Package.on_use(function (api, where) {
7 7
   //api.use(["jquery", "json"]);
8 8
   
9 9
   where = where || ['client', 'server'];
10  
-  api.use('backbone', where);
  10
+  api.use('backbone', ['client', 'server']);
  11
+  api.add_files('build/backbone.sync.js', ['client', 'server']);
11 12
 });
12 13
 
13 14
 Package.on_test(function (api) {
14  
-  api.use(['minimongo', 'backbone'], 'client');
  15
+  api.use(['minimongo', 'backbone'], ['client', 'server']);
15 16
   api.use('tinytest');
16  
-  api.add_files('backbone.sync.tests.js', 'client');
  17
+  api.add_files('build/backbone.sync.tests.js', ['client', 'server']);
17 18
 });
7  package.json
... ...
@@ -0,0 +1,7 @@
  1
+{
  2
+  "name": "Meteor smart package for backbone syncing",
  3
+  "version": "0.0.1",
  4
+  "devDependencies": {
  5
+    "grunt-contrib-coffee": "~0.3.2"
  6
+  }
  7
+}
7  smart.json
@@ -4,5 +4,8 @@
4 4
   "homepage": "https://github.com/jpka/meteor-backbone-sync",
5 5
   "author": "Juan Pablo Kaniefsky",
6 6
   "version": "0.0.1",
7  
-  "git": "https://github.com/jpka/meteor-backbone-sync.git"
8  
-}
  7
+  "git": "https://github.com/jpka/meteor-backbone-sync.git",
  8
+  "meteor": {
  9
+    "branch": "v0.5.2"
  10
+  }
  11
+}

0 notes on commit b0f4cfa

Please sign in to comment.
Something went wrong with that request. Please try again.