Implementing a factory pattern in enyo

I'm trying to implement a factory pattern for testing (and offline development) for my app. I thought by generating data in fetch() and this.merge-ing it, didFetch() would receive said factory-data. Yet, didFetch() is receiving data from this.url

Looking at the call stack, I only see framework calls (until my custom .didFetch())... so that doesn't help me understand why didFetch() is receiving url data vs the generated factory-data in .fetch()

Any suggestions as to a... better approach to implement a factory pattern?

Comments

  • Hard to tell without a code sample but perhaps his example will help. Built it for a different question but is using a mock source to generate data.

    If you're use the default ajax source, you can override it with something like this:
    enyo.store.addSources({
        ajax: "ex.MockSource"
    });
    Hope that helps
  • edited May 2014
    Wouldn't adding another source yield more of a fixture pattern or require a server-side app to generate the random data?

    The point of a factory is to generate random data upon instantiation. In this case, for example, a text attribute within the model would have lorem-ipsum style data and it's collection would have N instances of the model (where N is choses upon collection instantiation). In enyo.Collections, this would require one to "hijack" i.e. prevent calls to a server. Think of offline development.

    http://addyosmani.com/resources/essentialjsdesignpatterns/book/#factorypatternjavascript
  • Probably would have helped to have actually included the code.

    http://jsfiddle.net/ryanjduffy/HAYy7/1/

    Ultimately, I think you'll want to override at the source rather than at the collection or model. That's the right separation of concerns, imo. Otherwise, you'll have 2 places where data is injected into the system and that could lead to smoe unnecessary complexity.

    If you wanted to use the factory pattern to create the source so that you could more easily inject an alternate implementation, that's certainly possible. You can use enyo.store.addSources to set whatever the desired source is at runtime.
  • edited May 2014
    Any ideas as to why didFetch() is receiving data that is not coming from fetch()? (Just trying to understand the behavior I created / saw.)


    Also: Can't jsfiddle anymore:
    Blocked a frame with origin "http://fiddle.jshell.net" from accessing a cross-origin frame.
  • Can you share the code you were using to generate the data? I have a couple hypotheses but the code would be telling.
  • It's a part of a much larger project. Extracting only the relevant parts may be... more work than is worth satisfying my curiosity.... especially given the pattern provided in your jsfiddle... and "Modifying this array may have undesirable effects." -- http://latest.enyojs.com/api-tool/#enyo.Collection::records

    This is likely one of those effects.
Sign In or Register to comment.