pjs
pjs copied to clipboard
Private Variables dont match the Instance
Consider the following classes
var CrudPage = P(function(crud)
{
var self = crud;
crud.container = '#none';
crud.init = function() {
self = this;
$(document)
.on('click', function()
{
alert(self.container);
});
}
}
var UserPage = P(CrudPage, function(user, crud)
{
movimentacao.container = '#user';
});
var ProductPage = P(CrudPage, function(product, crud)
{
movimentacao.container = '#product';
});
All fine. Loved the implementation.
Lets go to the problem, thats occours on instantiation
# we create the instances
var user = UserPage();
var product = ProductPage();
# and trigger our test click
$(document).trigger('click');
The private self
, used to reference the Class out of the this
scope, got overwritten by each instance. So the events don't know UserPage
anymore.
I'm not sure what you're trying to do, but any var you put in the definition function is gonna be class-scoped, not instance-scoped.
for private methods, I suggest making self
a parameter and passing in this
from public methods.
In your example, I would try
crud.init = function() {
var self = this;
$(document).on('click', function() { alert(self.container) });
};
(note the additional var
)
That way the self
variable isn't shared with every instance
Yes, thanks, I have got that. But trying currently to overlap P in order to create a new instance (for the cases it is convenient).
do you think is that possible?
I'm not sure what you mean.
Think I have got it, please take a look https://gist.github.com/thiagof/01bcbab8f2041fa26a7b
Please correct me of any concept misunderstanding :)
P.js already doesn't require you to use new
... I'm not sure what the gain there is.
@thiagof: just to be clear,
var CrudPage = P(function(crud)
{
- var self = crud;
crud.container = '#none';
crud.init = function() {
- self = this;
+ var self = this;
$(document)
.on('click', function()
{
alert(self.container);
});
}
}
var UserPage = P(CrudPage, function(user, crud)
{
movimentacao.container = '#user';
});
var ProductPage = P(CrudPage, function(product, crud)
{
movimentacao.container = '#product';
});
does solve your problem, right? Then, what cases do you think your idea, even if you got it to work, would be more convenient? In this case, for example, our suggested canonical way to use Pjs is actually less code.
I think @jneen and I have no idea what you mean by "overlap P in order to create a new instance".
@thiagof so I think what you are trying to accomplish is Java-style private instance variables, yes?
P.js does not support these, and intentionally so. Your solution would involve creating a new class for every instantiation of an object, which I'd rather not do in Pjs. If I need private member variables, I usually prefix their names with _
.
I think problem migth be with javascript closure scope variables - i do not got my idea to work.
Yes, what works is redefine self in each function a event is registred (creating the closure).
Create isolate class instances would be benefical for designing a more reliable class isolation pattern (when needed). But or this isnt possible with javascript or I didnt have luck with cloning the objects before proto creation.
Han [email protected] escreveu:
@thiagof: just to be clear,
crud.init = function() { var self = this; $(document).on('click', function() { alert(self.container) }); };
and omitting the
var self = crud;
does solve your problem, right? Then, what cases do you think your idea, even if you got it to work, would be more convenient?
Reply to this email directly or view it on GitHub: https://github.com/jneen/pjs/issues/31#issuecomment-65873305