standardize watch feature

# memolus at googlemail.com (16 years ago)

developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Object:watch

I really do need it for my Modell-View-Controller application. If didn't had it, I had to use

modell.doSth(); modell.x++; modell.save();

where modell.save() will call the controller to reload all properties into view. With the watch feature I'm saving the last line. Property changings on the modell affects the view instantly.

May you can introduce the feature as part of a new event concept.

# Mike Samuel (16 years ago)

ES5 Setters let you do this.

Object.defineProperty( model, 'x', { 'set': function (newx) { model.x_ = x; }, 'get': function() { return model.x_ }, writable: true })

2009/12/27 memolus at googlemail.com <memolus at googlemail.com>:

# memolus at googlemail.com (16 years ago)

I would have to use 2 names per attribute "x" and "x_". That's crazy. I would have to use an library which does make this job for me.

I can't write an model with

Object.defineProperty( model, 'x', { 'set': function (newx) { model.x_ = x; object1.signal("x"); object2.signal("x"); object3.signal("x") }, 'get': function() { return model.x_ }, writable: true })

2009/12/27 Mike Samuel <mikesamuel at gmail.com>:

# Mike Samuel (16 years ago)

2009/12/27 memolus at googlemail.com <memolus at googlemail.com>:

I would have to use 2 names per attribute "x" and "x_". That's crazy. I would have to use an library which does make this job for me.

Or one for a backing store and the same name in that backing store.

I can't write an model with

Object.defineProperty(    model, 'x',    { 'set': function (newx) {  model.x_ = x; object1.signal("x"); object2.signal("x"); object3.signal("x") },      'get': function() { return model.x_ }, writable: true })

I don't know what you mean by model, why libraries are insufficient, or why various alternatives don't work. I also don't know why Object.watch works but property accessors don't, and how you work around problems with multiple watchers clobbering each other.

If you want to post a strawman that describes the problem in detail, explains non-language-changing alternatives and why they are insufficient, an idea of how many developers run into this problem, and details the syntactic changes and their semantics; then I and others on this list would be happy to take a look.

strawman:strawman is a good place to find examples.

# Jeff Watkins (16 years ago)

On 27 Dec, 2009, at 6:12 AM, memolus at googlemail.com wrote:

developer.mozilla.org/en/Core_JavaScript_1.5_Reference:Global_Objects:Object:watch

I really do need it for my Modell-View-Controller application. If didn't had it, I had to use

That's funny, I didn't need it when I wrote the Coherent MVC library. There's no explicit call to update the UI, it's all achieved through the magic available in ES3. ES3! Not the goodness of ES5. (After all, it still has to work in IE.)

modell.doSth(); modell.x++; modell.save();

where modell.save() will call the controller to reload all properties into view. With the watch feature I'm saving the last line. Property changings on the modell affects the view instantly.

Now, you'll only be able to detect changes to raw properties on ES5 browsers, because IE doesn't support getters and setters, yet. But if you're willing to recraft your code to look like the following:

model.doStuff();
model.setX(model.getX()+1);

You'll be just fine on ES3 browsers.