Skip to content

Commit

Permalink
Progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpka committed Dec 14, 2012
1 parent 7da77f5 commit b0f4cfa
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -1,2 +1,4 @@
.meteor/ .meteor/
smart.lock smart.lock
node_modules/
build/
53 changes: 38 additions & 15 deletions backbone.sync.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,23 +5,42 @@ methodTable =
delete: "destroy" delete: "destroy"


Backbone.miniMongoSync = (method, model, options, error) -> 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 # Backwards compatibility with Backbone <= 0.3.3
if typeof options == "function" if typeof options is "function"
options = options =
success: options success: options
error: error 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) -> syncDfd = $? and $.Deferred and $.Deferred() #If $ is having Deferred - use it.
if error
options.error "Record not found" #if Meteor.is_client
syncDfd.reject() if syncDfd try
else coll[methodTable[method]] model.attributes, (error, id) ->
options.success model if error
syncDfd.resolve() if syncDfd 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 # switch method
# when "read": # when "read":
Expand All @@ -39,10 +58,14 @@ Backbone.miniMongoSync = (method, model, options, error) ->


Backbone.ajaxSync = Backbone.sync Backbone.ajaxSync = Backbone.sync


# Override 'Backbone.sync' to detect if it is a minimongo model/collection or not #Decide which sync method to use
# and decide a sync method accordingly Backbone.getSyncMethod = (model) ->
Backbone.sync = (method, model, options, error) -> if Meteor.is_server or model.mCollection or (model.collection and model.collection.mCollection)
if model.db or (model.collection and model.collection.db)
Backbone.miniMongoSync Backbone.miniMongoSync
else 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)
124 changes: 107 additions & 17 deletions backbone.sync.tests.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,26 +1,116 @@
Tinytest.add "Backbone.sync - defaults to ajax", (test) -> if Meteor.is_server
m = new Backbone.Model() Tinytest.add "Backbone.sync - uses only mm", (test) ->
m = new Backbone.Model()


#remoteModel should use ajax sync #remoteModel should use ajax sync
test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync


#Backbone.sync should return a value when ajax is used." M = Backbone.Model.extend
test.notEqual m.fetch({url: "/"}), undefined mCollection: "collection"
m = new M()


# module("Backbone.sync", _.extend(new Environment, { test.equal Backbone.getSyncMethod(m), Backbone.miniMongoSync


# setup : function() { if Meteor.is_client
# Environment.prototype.setup.apply(this, arguments); Tinytest.add "Backbone.sync - defaults to ajax", (test) ->
# library = new Library; m = new Backbone.Model()
# library.create(attrs, {wait: false});
# },


# teardown: function() { #remoteModel should use ajax sync
# Environment.prototype.teardown.apply(this, arguments); test.equal Backbone.getSyncMethod(m), Backbone.ajaxSync
# Backbone.emulateHTTP = false;
# }


# })); #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() { # test("read", 4, function() {
# library.fetch(); # library.fetch();
Expand Down
56 changes: 56 additions & 0 deletions grunt.js
Original file line number Original file line Diff line number Diff line change
@@ -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");
};
7 changes: 4 additions & 3 deletions package.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ Package.on_use(function (api, where) {
//api.use(["jquery", "json"]); //api.use(["jquery", "json"]);


where = where || ['client', 'server']; 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) { Package.on_test(function (api) {
api.use(['minimongo', 'backbone'], 'client'); api.use(['minimongo', 'backbone'], ['client', 'server']);
api.use('tinytest'); api.use('tinytest');
api.add_files('backbone.sync.tests.js', 'client'); api.add_files('build/backbone.sync.tests.js', ['client', 'server']);
}); });
7 changes: 7 additions & 0 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Meteor smart package for backbone syncing",
"version": "0.0.1",
"devDependencies": {
"grunt-contrib-coffee": "~0.3.2"
}
}
7 changes: 5 additions & 2 deletions smart.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"homepage": "https://github.com/jpka/meteor-backbone-sync", "homepage": "https://github.com/jpka/meteor-backbone-sync",
"author": "Juan Pablo Kaniefsky", "author": "Juan Pablo Kaniefsky",
"version": "0.0.1", "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.