Last active
February 4, 2016 11:15
-
-
Save tenbits/10994141 to your computer and use it in GitHub Desktop.
Class Store. Same interface for XHR Transport and MongoDB driver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// atma-class | |
// common node.js and browser | |
var Article = Class('Article', { | |
Base: Class.Serializable({ | |
date: Date | |
}), | |
_id: null, | |
date: null, | |
body: '', | |
Validate: { | |
body: function(value) { | |
if (!value) | |
return 'Should not be empty'; | |
} | |
} | |
}); | |
// in node.js | |
// define mongo db once | |
Class.MongoStore.settings({ | |
db: 'my-blog-site' | |
}); | |
// then update your entities with MongoStore attribute | |
var Article = Class.patch('Article', { | |
Store: Class.MongoStore.Single('blogs') | |
}); | |
// in browser | |
var Article = Class.patch('Article', { | |
Store: Class.Remote('/rest/blogs/?:_id') | |
}); | |
// Now you can work with the Article in nodejs and browser environments: | |
// in browser: serialized data is sent via Ajax to the server, @see Remote | |
// in nodejs: serialized data is saved/updated/patched/remove to/fom the mongodb | |
var article = new Article({ | |
body: 'Hello world' | |
}) | |
.save() | |
.done(function(article) { | |
console.log('saved', article._id); | |
}) | |
// other methods | |
article.patch({ | |
$set: { | |
date: new Date | |
} | |
}); | |
var article = Articel | |
.fetch({ | |
_id: ID, | |
date: { | |
$gte: new Date() | |
} | |
}) | |
.done(function(article){ | |
// | |
}) | |
.fail(onError) | |
; | |
article | |
.del(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment