I couldn't find an example on how to have a custom function inherit jQuery's functions while leaving the original jQuery function I'm inheriting from unchanged (I wanted my function to have a read
but didn't want it to get added to $.Deferred
)
This works, but I really suspect it isn't the right way to do it.
var DeferredFileEntry = function(){
var def = $.Deferred();
var promise = def.promise;
def.read = function(textFnCallback){ // <--this is the method I'm adding
return $.Deferred(function(newDefer){
var newDefer = $.Deferred();
def.done(function(fileEntry){
fileEntry.getText()
.done(function(text){
textFnCallback(text);
newDefer.resolve(fileEntry);
});
});
}).promise();
};
def.promise = function(){
//expose the custom method to the returned promise
promise.read = def.read;
return promise();
};
return def;
};
An example usage:
var getFileEntry = function(){
var def = DeferredFileEntry();
libraryCall() // that promises to resolve with a FileEntry object
.done(function(fileEntry){
def.resolve(fileEntry);
});
return def.promise();
}
getFileEntry()
.read(function(text){
console.log("reading content:" + text);
})
.read(function(text){
//chained,
//doesn't run until the previous one has resolved
console.log("reading content again: "+text);
});
getFileEntry
? Does it just returnDeferredFileEntry
? \$\endgroup\$function (newDefer) { var newDefer = $.Deferred();
\$\endgroup\$