Skip to content

Instantly share code, notes, and snippets.

View vseventer's full-sized avatar

Mark van Seventer vseventer

View GitHub Profile
@vseventer
vseventer / callable-object.js
Last active July 10, 2016 10:31
Create a callable object in JavaScript which returns an isolated clone of itself. This is *not* a class, hence using the new keyword has no effect.
// How to define:
// --------------
var callableObjectFn = function() {
// Use CallableObject as name in constructor function, but also when setting property below.
var CallableObject = (function(CallableObject) {
return function() {
return CallableObject();
};
}(callableObjectFn));
CallableObject.property = 'value';
@vseventer
vseventer / gist:3537809
Created August 30, 2012 19:05
Facebook authentication with Kinvey.
// Initialize the Facebook SDK.
FB.init({
appId: '<your-app-id>',
oauth: true
/* additional properties */
});
// Open login popup.
FB.login(function(response) {
if(response.authResponse) {
@vseventer
vseventer / gist:3537784
Created August 30, 2012 19:04
Facebook authentication.
// Initialize the Facebook SDK.
FB.init({
appId: '<your-app-id>',
oauth: true
/* additional properties */
});
// Open login popup.
FB.login(function(response) {
if(response.authResponse) {
@vseventer
vseventer / gist:3258533
Created August 4, 2012 16:01
Setting the store for a custom collection.
// "items" is the collection name.
var Item = Kinvey.Entity.extend({
constructor: function(attributes) {
Kinvey.Entity.prototype.constructor.call(this, attributes, 'items', {
store: 'offline',
options: { policy: 'cachefirst' }
});
}
});
var Items = Kinvey.Collection.extend({
@vseventer
vseventer / gist:3217837
Created July 31, 2012 15:29
Kinvey JavaScript Library: Loading the shim.
...
<script>
window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || document.write('<script src="IndexedDBShim.min.js"><\/script>');
</script>
<script src="//da189i1jfloii.cloudfront.net/js/kinvey-js-0.9.8.min.js"></script>
</body>
</html>
@vseventer
vseventer / gist:3217830
Created July 31, 2012 15:29
Kinvey JavaScript Library: conflict resolution policy.
Kinvey.Sync.configure({
conflict: Kinvey.Sync.serverAlwaysWins
/* or:
conflict: Kinvey.Sync.clientAlwaysWins
*/
});
@vseventer
vseventer / gist:3217822
Created July 31, 2012 15:28
Kinvey JavaScript Library: Using the offline store.
var entity = new Kinvey.Entity({/*attributes*/}, 'collection', {
store: 'offline',
options: { policy: 'cachefirst' }// Optional, caching policy.
});
var collection = new Kinvey.Collection('collection', {
store: 'offline',
options: { policy: 'cachefirst' }// Optional, caching policy.
});
@vseventer
vseventer / gist:3217821
Created July 31, 2012 15:27
Kinvey JavaScript Library: Initialization for offline usage.
Kinvey.init({
appKey: '<your-app-key>',
appSecret: '<your-app-secret>',
sync: true
});
@vseventer
vseventer / gist:3217817
Created July 31, 2012 15:27
Kinvey JavaScript Library: Leaderboard example.
var collection = new Kinvey.Collection('scores', {
store: 'cached',
options: { policy: 'cachefirst' }
});
// The leaderboard should override the collections cachefirst policy.
var leaderboard = new Kinvey.Aggregation().on('player');
collection.aggregate(leaderboard, {
policy: 'nocache',// Overrides cachefirst policy.
success: function(response) {
@vseventer
vseventer / gist:3217803
Created July 31, 2012 15:26
Kinvey JavaScript Library: Setting the caching policy.
var entity = new Kinvey.Entity({/*attributes*/}, 'collection', {
store: 'cached',
options: { policy: 'cachefirst' }
});
entity.load('my-entity', {
success: function(response) {
// response is the entity instance.
}
});