petermichaux / maria

The MVC framework for JavaScript applications. The real MVC. The Smalltalk MVC. The Gang of Four MVC.
BSD 2-Clause "Simplified" License
764 stars 51 forks source link

expose evento.EventTarget #67

Closed viking closed 11 years ago

viking commented 11 years ago

I would like to mixin EventTarget into my own objects. Can the evento.EventTarget be exposed through the maria object?

viking commented 11 years ago

Or is it better to just use Model?

petermichaux commented 11 years ago

The evento.EventTarget object could be exposed through the maria object and I'm ok with doing that. (Ironically when the evento, hijos, etc objects were exposed as their own objects earlier in the life of Maria, folks wanted them hidden. Now that they are hidden, folks want them exposed.)

How is it that you want to use the EventTarget? If it is for an object that can be considered a model then using maria.Model.subclass is what I recommend.

viking commented 11 years ago

Ah, I was curious about that actually. It seemed like those other namespaces were designed to be exposed.

I've implemented a storage class for persistence that utilizes localStorage. I want to be able to observe changes in a SetModel and react accordingly.

I ended up creating a proxy class to handle the events. It's just a regular class that implements the handleEvent method. Seems to work well. I could have just used a function to react to model events, but I wanted to be able to turn off reacting to events easily.

Here's what I did: https://github.com/viking/collect-js/blob/6e7f3dae40af5212d7647ed88b448519b6459c88/src/js/persistence/local.js

petermichaux commented 11 years ago

In your handleEvent function, there is no handling if there are evt.removedTargets. Is that not possible in your model?

For TodoMVC, I did not worry about adding and removing individual to-do items from the local store. If there was a change in the model layer I just serialized the whole model layer and stored it again. Probably not very scalable but it was not a performance problem for an app with a few handfuls of to-do items. See https://github.com/tastejs/todomvc/blob/gh-pages/architecture-examples/maria/js/bootstrap.js#L16

I'm curious about your use of setTimeout. Is that just to allow the JavaScript engine a break since your API is asynchronous anyway? Or are you more concerned about not delaying the UX?

viking commented 11 years ago

As for the removedTargets, I just haven't gotten around to that yet. I'll add it soon.

Mainly I decided to use setTimeout because I want to create a remote storage class that has the same API as the local storage, and since that will be asynchronous, I thought it would be good to be consistent. But I like the idea of not delaying the UX, too.

petermichaux commented 11 years ago

Thanks for the extra info.

Please reopen if you have more questions pertaining to this issue.