chaplinjs / chaplin

HTML5 application architecture using Backbone.js
http://chaplinjs.org
Other
2.85k stars 232 forks source link

Change base class extensions #169

Closed thomasconner closed 12 years ago

thomasconner commented 12 years ago

I love this library and would like to continue to use it for my projects but I keep running into a major issue. Since chaplin.js is dependent on backbone.js and its base/core modules, you can not add backbone.js enhancements from other third party libraries. For example, I would like to you Backbone-relational by PaulUithol (Backbone-relational) but simply cannot without editing the src for model and collections in chaplin.js. I believe there could be a more elegant way. Simply having a file that informs chaplin.js where to inherit from would help. Something like the following:

Chaplin.extensions = {
   model: Backbone.RelationalModel,
   collection: Backbone.RelationalCollection,
   view: Backbone.View,
   // And so on
};

// In the Chaplin.Model
define [
  'underscore',
  'backbone',
  'chaplin/lib/utils'
  'chaplin/lib/subscriber'
  'chaplin/lib/sync_machine'
], (_, Backbone, utils, Subscriber, SyncMachine) ->
  'use strict'

  class Model extends Chaplin.extensions.model

  // Rest of model definition ..... and so on

Any thoughts or ideas? Let me know if this is possible. Thanks and keep up the good work.

molily commented 12 years ago

Chaplin relies on AMD so the configuration should be an AMD module. It needs to be defined before the Chaplin module is loaded so it can try to require() it. Don’t know how AMD module loaders handle this, but I’ll investigate. In general that seems to be kind of a hack, probably someone comes up with a better idea.

In JavaScript there are several ways to create pseudo-classes dynamically, of course. For example a class factory which creates a custom Model class based on a given superclass. But we would need to dump the CoffeeScript class declarations for full flexibility.

In general, I’m aware that the concept of CoffeeScript pseudo-classes has several limitations. For example, if you’re not using CoffeeScript, super calls are tedious, and what you really want is AOP-style method composition. That’s easily possible because of the functional nature of JavaScript, but it doesn’t fit into the classical inheritance mold. Also, you will need multiple inheritance, traits or at least mixins sooner or later. After all, CoffeeScript classes add a lot of convenience, readability and conventions to the code, so at the moment I don’t want to dump them completely.

thomasconner commented 12 years ago

Yea I agree and thats why I was looking for some input. Thanks for responding.

thomasconner commented 12 years ago

Figured out a solution and will discuss later.

bez4pieci commented 12 years ago

@ThomasConner I'm having the same trouble — would like to hear your solution.