Hi Wil,
info.key.info.value(this)); have two closed brace, but I'm sure that
it's a cut & paste error :))
Something surprise me, you call info.key.info.value as an object and
you say that info.key and info.value is defined. It's strange, because
you should have say info.key.info.value is an existing object.
But, the real problem is that you call this inside an anonymous
function:
observerInfo.each(function(info) {
info.key.info.value(this));
});
this should refer to your "objectOfMyObservableClass"? is that rigth ?
But inside the anonymous function, it lost its binding to the defined
class and refer the function (so in fact is the window object)
To keep the reference to this inside the anonymous function, there is
a first way:
var that=this;
observerInfo.each(function(info) {
info.key.info.value(that));
});
And another way could be done in a more prototypish way:
var myFunction=(function(info) {
info.key.info.value(this));
}).bind(this);
observerInfo.each(myFunction);
--
david
On 3 déc, 22:00, Wil <[email protected]> wrote:
> Hi all,
>
> I'm trying to implement a somewhat generic way for my controller
> objects to listen for changes to various properties of my model
> objects. I'm not a javascript expert, so there is quite possibly a
> Better Way™ of solving the bigger problem - I'm open to pointers in
> that direction, if my current approach is not a good idea.
>
> Here's the machinery I currently have in place:
>
> MyClass.prototype.observeValueChange = function(observer, property,
> callback) {
> if (this.observableChanges == null) this.observableChanges = new
> Hash();
> if (this.observableChanges.get(property) == null)
> this.observableChanges.set(property, new Hash());
> this.observableChanges.get(property).set(observer, callback);
>
> }
>
> MyClass.prototype.fireValueChanged = function(property) {
> if (this.observableChanges == null) return;
> var observerInfo = this.observableChanges.get(property);
> if (observerInfo == null) return;
> observerInfo.each(function(info) {
> info.key.info.value(this));
> });
>
> }
>
> Here's an example of registering to listen:
>
> objectOfMyObservableClass.observeValueChange(this, 'foo', function
> (destination) {
> alert("observed change in foo");
>
> });
>
> objectOfMyObservableClass.fireValueChanged("foo");
>
> Tracing through the code, everything appears fine up to the point
> where I try to call the stored callback. This line:
>
> info.key.info.value(this));
>
> info.key and info.value are both valid objects, but nothing happens.
>
> Thanks in advance!
--
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en.