I want to use the execAsync function here:
https://developer.mozilla.org/en/Storage#Asynchronously
and I want to pass values between handleResult and handleCompletion. Something like
statement.executeAsync({
handleResult: function(aResultSet) {
VALUE = 1
},
handleCompletion: function(aReason) {
print(VALUE);
}
});
What's the best way to do it?
var value;
statement.executeAsync({
handleResult : function(aResultSet) {
value = 1;
},
handleCompletion : function(aReason) {
print(value);
}
});
Well the obvious thing to notice is that you're passing an object to executeAsync. (In particular, it's a mozIStorageStatementCallback, so it should have a handleError method too.) So you can easily associate properties specific to that object with the object, using the "this" keyword:
statement.executeAsync({
value: 1,
handleResult: function(aResultSet) {
this.value = 0;
},
handleError: function(aError) {
this.value = 2;
},
handleCompletion: function(aReason) {
print(this.value);
}
});
Related
With this polymer element definition:
<script>
Polymer({
is: 'hello-world',
properties: {
dataId: {
type: String
},
value: {
type: String
}
},
ready: function() {
console.log(this.dataId);
registerCallback(this.dataId, function (data) {
var z = data.someValue;
this.value = z;
});
}
});
</script>
When it tries to set this.value, this is actually the data, not the element. I want it to set the polymer property "value" to the value of z. How can I do this?
Here is the registerCallback() if needed:
function registerCallback(id, callback) {
callbackMap[id] = callback;
}
There are two ways of doing it
As you already know this has a different meaning inside the callback so one way would be to store this(Polymer element) inside some other variable
var self=this;
registerCallback(function(){
self.data = some value
})
Second option is to bind this with your callback
registerCallback(function(){
this.data= some value
}.bind(this));
This might be a stupid problem, but I have an object in which I wanted to store references of other functions/variables and later use them.
For example,
var obj = {
keyA: {
ref: window.objects.someAAA
},
keyB: {
ref: window.objects.someBBB
}
}
Here, when I set this object, window.objects is empty, so accessing those keys (someAAA and someBBB) will give undefined.
I know I was dumb when I first thought of assigning them like this and expecting their actual values after they're loaded, to be available via obj.keyA and obj.keyB. But obviously, they still contain undefined.
But is there a way to do that! I know if I set string instead of reference, I can eval, but any better solution?
Think this should work
var obj = {
keyA: {
ref: function() { return window.objects.someAAA; }
},
keyB: {
ref: function() { return window.objects.someBBB; }
}
}
You can then access via
obj.keyA.ref()
Test
Add the code above then do
window['objects'] = {};
window['objects']['someAAA'] = 'Hallo';
obj.keyA.ref() // will Output 'Hallo'
Try using Getters for such case:
var obj = {
get keyA(){
return (window['objects'])? window.objects.someAAA : null
},
get keyB() {
return (window['objects'])? window.objects.someBBB : null
}
}
// as now window.objects doesn't exists
console.log(obj.keyA); // outputs "null"
window.objects = {'someAAA': "aaa-value", 'someBBB': "bbb-value"};
console.log(obj.keyA, obj.keyB); // outputs "aaa-value" "bbb-value"
I'm wondering about what's happening when you end up doing something like this in a javascript function:
function() {
var privateMembers : {
'methodA' : function() {
},
'methodB' : function() {
}
};
var publicMembers = {
'methodC' : function() {
},
'methodD' : function() {
}
};
publicMembers.listen = {
"myEvent:Listener" : publicMembers.MethodC;
};
return publicMembers;
}
I understand that returning just publicMembers makes the methods available, however, I'm also wondering would that also make the publicMembers.listen available as well?
yes. these two codes are equivalent
var publicMembers = {
'methodC' : function() {
},
'methodD' : function() {
}
};
publicMembers.listen = {
"myEvent:Listener" : publicMembers.MethodC;
};
and:
var publicMembers = {
'methodC' : function() {
},
'methodD' : function() {
}
'publicMembers' : {
listen : {
"myEvent:Listener" : publicMembers.MethodC;
}
}
};
publicMembers.listen is just a property of publicMembers object, there is no difference between declaring properties using object literals or dot notation. So, when you add listen property to publicMembers object, and you return the whole object, surely you will have access to publicMembers.listen as well. It's pretty easy to test btw.
My question:
var nsPreferences = {
property1:"",
get mPrefService()
{
return Components.classes["#mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
},
setBoolPref: function (aPrefName, aPrefValue)
{
try
{
this.mPrefService.setBoolPref(aPrefName, aPrefValue);
}
catch(e)
{
}
},
getBoolPref: function (aPrefName, aDefVal)// Prefs.jsで使用
{
try
{
return this.mPrefService.getBoolPref(aPrefName);
}
catch(e)
{
return aDefVal != undefined ? aDefVal : null;
}
return null; // quiet warnings
},
};
In this object nsPreferences, what is this "get mPrefService(){}"? This is the first time I've seen this kind of syntax in javascript object. Would anyone tell me about this syntax?
It's a getter function. It will look like a variable when you read it:
var someService = nsPreferences.mPrefService;
It calls that function without using the regular invocation parens. You can also use the set operator to create a "setter" function for the same property:
set mPrefService(val){
this.actualVal = val;
},
nsPreferences.mPrefService = "service";
I am trying to give an object a countdown timer and when the countdown is down it should call a function which removes this object from an array.
array = [];
var object = {
'name' : 'user',
'limit' : function() {
setTimeout(destroyMe(this),10000);
}
}
array.push(object);
var destroyMe = function(obj) {
array.remove(obj);
}
I know that there could a problem with the this but the timeout function doesnt work at all not even like this:
var object = {
'name' : 'user',
'limit' : function() {
setTimeout(console.log("dd"),3000);
}
}
Maybe someone can tell me the issue with my version of the setTimeout.
thx
setTimeout takes a reference to a function. The code you have is calling the function.
This should be changed to:
var object =
{
'name' : 'user',
'limit' : function()
{
setTimeout(function() { destroyMe(this); }, 10000);
}
}
(You may have issues using this in this context, try it!)
var object =
{
'name' : 'user',
'limit' : function()
{
setTimeout( function() { console.log("dd"); },3000);
}
}
You need to pass a function to setTimeout, not a function call.
var object = {
'name' : 'user',
'limit' : function() {
setTimeout(function() {
destroyMe(this)
}, 10000);
}
};
As you already know, the this probably doesn't do what you are expecting. Replacing destroyMe(this) with console.log("dd") should result in a behavior that you would expect.
setTimeout either takes a callback (a function, not a function call with arguments) or code in quotes. However, as noted in this question, passing a string is only allowed for historical reasons--there aren't any good reasons for doing it in practice.
But, for completeness sake, one way to do what you want is:
var object = {
'name' : 'user',
'limit' : function() {
setTimeout('console.log("dd")', 3000);
}
}