asciidisco / asciidisco/Backbone.Mutators

Integrating mutators with backbone.relational

Open
#37 1 comment 0 reactions 1 assignee Claimed by @asciidisco View on GitHub
enhancement question
Dominant language
JavaScript
Stars
227
Forks
27
PR merge metrics
No merged PRs in 30d

Description

I've faced the following issue while trying to integrate Backbone.Mutators with Backbone.Relational which is used to build client side relations and helps to serialize/deserialize data into corresponding json/models.

For example lets assume we have a model (in coffeescript)

``` coffeescript
@ClientApp.module "Entities", (Entities, App, Backbone, Marionette, $, _) ->
class Entities.Store extends Entities.RelationalModel

defaults:
location: -> new Entities.Location()

mutators:
store_name: ->
@get("location")?.get("name")
```

What I have now is trying to access related model in mutator will result in an error. That is because backbone.relational haven't yet converted the model's sub-object into an instance of underlying model at the time backbone.mutators serialize data and hence `@get('location')` will return a json object instead of Entities.Location instance.
I've applied a quick fix by changing the library to save `get` and `set` methods from Backbone.RelationalModel.

``` javascript
var Mutator = function () {},
oldGet = Backbone.RelationalModel.prototype.get,
oldSet = Backbone.RelationalModel.prototype.set,
oldToJson = Backbone.RelationalModel.prototype.toJSON;

...

_.extend(Backbone.RelationalModel.prototype, Mutator.prototype);
```

However i do not really like this fix as far as i will have to apply it every time Backbone.Mutators is updated. Perhaps someone has a more consistent solution? Also please let me know if you need any additional info.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.