-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addComputed() syntax could be more elegant #209
Comments
For inspiration (and history of what was used as a basis for current implementation), check out https://github.com/NYTimes/backbone.stickit |
Okay, so another issue is that defining the mappings require the actual object models to be present at the time of defining them, which is hard/impossible to do from the class level. Right now, you can define the mappings by passing them in as arguments during initialization (because assumingly when you create the form model, you have access to the object models) or you call addModel and addComputed later. There is a little known third option called "mapping" which is actually mislabeled as "defaultMapping" in the docs. You can set this but only if you have access to the object model when you are doing your class definition - which is like never. Here's my proposed solution to both not being able to define mappings on a class level and hopefully cleaning up the syntax: rely on named object models on the form model. Here's what it would look like:
Couple of notes both good and bad on it:
Examples for Pull and Push method alternatives: PULLA)
B)
PUSHA)
B)
|
Initial thoughts:
The methods for updating models also allows you to write a generalized form model that might attached to different types of object models in different contexts and then pull/push would only affect the object models that were added by the view before the pull/push. |
The only thing that complicates always using the aliases is when we "startUpdating" and we set listeners on them. We could try to prevent redefining aliases when the formmodel is in the updating process? or just have that be something where we tell people not to update an alias during active updates. |
I was thinking you would explicitly bind an alias to an object model instance at some point (via options, class vars or a method). When you bind it you setup listeners. |
or when you 'startUpdating' you setup listeners for the models you have. And set listeners for new models that are added when they are added if it is in the 'updating' state. |
Ah I see, I missed the point you mentioned needing methods to add new/replace exisitng models. I feel like we should either always use add/replace methods or always use view references. Doing both is confusing. If we can't "hotswap" new view references (which i'd like to think about how to do that), then we should always use add/replace methods.
(ps. i made this.addModel just pass back the model so I could set it as a ref. on view because people will want to save references to the models on the view anyway) |
note that we can do the addModel stuff for options. and this. in the form model's constructor. |
We have very 'programatic' use cases, what are are general cases that we are trying to optimize?
General cases of processes for binding object model:
I think multiple object models are important to support, but they don't need to be optimized for setting up. |
I'm leaning towards adding a
we can rename
|
Should also support: this.addMapping('newModel', ['foo', 'bar'], this.model2) |
We could also add a convenience method:
that could be used in the initialize method |
@mandragorn, agreed. And probably keep the "copy" parameter as well |
is myViewMethod meant to be a random method we add that the view would call? |
yes. though adding a mapping and setting a model could be done in separate calls. Or even having a mapping defined on the class level and much later in a random method you could set the model |
Im thinking of changing |
Simple case examples (to see verbosity):For binding in the FormView:
... there are more we should probably enumerate. What about a configuration object for addMapping() that is similar to mappings, computed, etc. from the form view definition. |
suggested additions:
if we don't specify an alias can we have a default one that is used? If only the default is used is it worth having push/pull just present the model itself as the top level? I'm thinking mainly about optimizing the single model case. |
Interesting. I was still thinking to use Also, your examples all assume you haven't defined a form model beforehand. Totally legit, but thought I'd point that out. |
yes, correct, defining the form model beforehand is another set of use cases. |
We could probably do a built-in alias. We could return the alias in case you wanted to add a computed on it later... |
duck-typing for 2nd var:
Other duck-typing (am I typing that correct? Its not like duct-typing like tape, right?)
|
unified mappings config object (either 2nd var or as the entire thing):
|
so at some complexity you switch to the full options object, but for minimal/simple configs we allow some polymorphism to the method parameters. |
couple of thoughts:
like backbone does with multiple events. When would you do unitified mappings? And are you suggesting that on the class level we still allow instance mapping like you've added with |
Also, your logic about duck typing is missing when there isn't a backbone model instance. That's the use case when are setting up the configurations without instances. |
sure I thought missing instances is implied since its the last thing in the list. I don't have a preference between array and space delimited. Arrays are easer to manage in javascript (since the type is what it is) while space delimited is more readable and easier to type. By unified mappings I just mean the full 'configuration' object who's pattern could be applied to different places. I.e. the content of it is the same as what is in the extend({}), the same object could be passed into addMappings() instead of multiple parameters for complex logic and finally it can be used as a template for the 2nd parameter (at least some subset of it?, maybe computed stuff can only be done through the full options object to addMappings()). |
…FormModel refactoring
Here's what I went with:
a model map looks like:
it can be assigned as the last parameter to setMappings or setMapping if its a computed. And this could be a way you initialize them:
or define a form model with its mappings without models
|
should the models mapping also have a boolean for each model for whether to pull immediately or do we drop that? I'm ok calling pull explicitly on the form model after setting up mappings. |
In the patch you can pass in a boolean at the end to setModel(s) or setMapping(s) to do a copy/pull of that model/mapping. If you meant passing in booleans during initialization, i'd rather not support that |
The addComputed() syntax is quite verbose for simple cases, and is a bit confusing even with the documentation. We should both fix up the documentation and think about changing the API to be cleaner.
The text was updated successfully, but these errors were encountered: