Last active
August 29, 2015 14:04
-
-
Save hisapy/10f44c1dc763e49fa553 to your computer and use it in GitHub Desktop.
Wrapping persistencejs methods to use jQuery Promises
This file contains hidden or 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
//This depends on jquery and persistencejs. | |
/** | |
* Given a"model" Product. Instances of Product persist their data to Web SQL (persistencejs) | |
*/ | |
var Product = persistence.define('products', { | |
name: 'TEXT', | |
image_path: 'TEXT', | |
created_at: 'DATE', | |
updated_at: 'DATE' | |
}); | |
/** | |
* If we want info from db, we do something like. | |
*/ | |
persistence.transaction( | |
function(tx){ | |
tx.executeSql( | |
"select max(updated_at) as max_updated_at from products", | |
null, | |
function(row){ | |
//this is a callback used to receive the results from db | |
} | |
); | |
} | |
); | |
/** | |
* Then we might want to encapsulate that logic into a method of our Product model. | |
*/ | |
Product.getMaxUpdatedAt = function(callback){ | |
var deferred = new $.Deferred(); | |
persistence.transaction( | |
function(tx){ | |
tx.executeSql( | |
"select max(updated_at) as max_updated_at from products", | |
null, | |
callback | |
); | |
} | |
); | |
}; | |
/** | |
* And use the method by creating a callback to get the query result | |
*/ | |
var aCallback = function(row){console.log(row);} | |
Product.getMaxUpdatedAt(callback); | |
/** | |
* But lets suppose we need to getMaxUpdatedAt() of several models synchrounously and do something only | |
* when all of them are fetched from db. We would end up writing something like | |
*/ | |
var maxs = []; | |
Product.getMaxUpdatedAt(function(productMax){ | |
maxs.push(productMax); | |
Category.getMaxUpdatedAt(function(categoryMax){ | |
maxs.push(categoryMax); | |
User.getMaxUpdatedAt(function(userMax){ | |
maxs.push(userMax); | |
//Notice how we can get deeper and deeper | |
maxs.sort(function(a,b){return a-b}) | |
}) | |
}) | |
}) | |
/** | |
* To avoid deeply nested callbacks we can use Promises. | |
* We can refactor the getMaxUpdatedAt() methods to use jQuery.Deferred() to return a Promise, just like $.ajax does. | |
*/ | |
Product.getMaxUpdatedAt = function(){ | |
var deferred = new $.Deferred(); | |
persistence.transaction( | |
function(tx){ | |
tx.executeSql( | |
"select max(updated_at) as max_updated_at from products", | |
null, | |
function(row){ | |
deferred.resolve(row[0]['max_updated_at']); | |
} | |
); | |
} | |
); | |
return deferred.promise(); | |
}; | |
/** | |
* So if we want to sort all the maxs ... | |
*/ | |
var maxResults = []; | |
maxResults.push(Category.getMaxUpdatedAt()); | |
maxResults.push(Product.getMaxUpdatedAt()); | |
maxResults.push(User.getMaxUpdatedAt()); | |
$.when.apply($, maxResults).then(function(maxCategory, maxProduct, maxUser){ | |
maxs = [maxCategory, maxProduct, maxUser].sort(function(a, b){return a-b}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment