Thanks for the replies; much appreciated.
I have a somewhat underlying question: In your apps, to apply the same functionality to different elements in the UI, do you ever create more than one instance of a particular control class? Or do you always extend it into subclasses and create one instance of each subclass?
Here's an example of what I mean:
I have a somewhat underlying question: In your apps, to apply the same functionality to different elements in the UI, do you ever create more than one instance of a particular control class? Or do you always extend it into subclasses and create one instance of each subclass?
Here's an example of what I mean:
- // Option A: One reusable control
- can.Control('CreateForm', {
- defaults: {
- listToAddTo: null,
- loadMask: "Creating...",
- afterCreate: null
- }
- }, {
- // functionality for form validation, model creation, loadmask, etc
- });
- // two instances: one form creates new domains, one creates new users
- new CreateForm($domainCreateForm, {
- listToAddTo: myListOfDomains,
- loadMask: "Creating Domain...",
- afterCreate: function(newDomain) { /* select newDomain */ }
- });
- new CreateForm($userCreateForm, {
- listToAddTo: myListOfUsers,
- loadMask: "Creating User...",
- afterCreate: function(newUser) { /* select newUser */ }
- });
That's what we have today -- but it's currently not covered by tests. We have other 'reused' ("thick"?) control classes for forms which have different functionality (like deleting models, handling lists, etc).
- // Option B: A base class with two single-use controls
- can.Control('BaseCreateForm', { // this is 100% identical to CreateForm, above
- defaults: {
- listToAddTo: null,
- loadMask: "Creating...",
- afterCreate: null
- }
- }, {
- // functionality for pulling field values for the model, creating the model, showing/hiding loadmask, etc
- });
- // Instead of two instances of one class, make one instance each of two classes
- // (Presumably, these live in separate files)
- BaseCreateForm('DomainCreateForm', {
- loadMask: "Creating Domain...",
- afterCreate: function(newDomain) { /* select newDomain */ }
- });
- BaseCreateForm('UserCreateForm', {
- loadMask: "Creating User...",
- afterCreate: function(newUser) { /* select newUser */ }
- });
- new DomainCreateForm($domainCreateForm, {
- listToAddTo: myListOfDomains
- });
- new UserCreateForm($userCreateForm, {
- listToAddTo: myListOfUsers
- });
The second form is what we're considering refactoring to, because it more cleanly separates 'innate' information (the loadmask and afterCreate callback) from 'instance' information (which dom element to use, which model.list to add to), and because it seems better suited for different test suites. We'll end up with a lot of new, nearly-empty js files (one for DomainCreateForm, one for UserCreateForm, etc), but I think it'll be worth it.
In both cases, though, nearly all of the actual functionality lives in one class, the instances themselves are created via a higher-level controller (with models/observes passed in), etc.
Thanks again,
Steven
In both cases, though, nearly all of the actual functionality lives in one class, the instances themselves are created via a higher-level controller (with models/observes passed in), etc.
Thanks again,
Steven