kmalakoff / kmalakoff/knockback

Manually created observables doesn't work if initially created with model = null

Open
#161 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.1k
Forks
69
PR merge metrics
No merged PRs in 30d

Description

This issue is a continuation of #82. In version 0.19.1 you changed the way `createObservables` work and also provided an excellent example on how to update my code for that fix, so huge thanks.

Unfortunately I didn't test this until now (I'm a consultant and have been away from this project for a while) and version 0.19.1 introduced a new problem if the model is initially set to `null` that prevents me from updating (and I really want to update for the memory fixes).

This example (almost the same as in #82) worked fine in 0.19.0:

```javascript
var children = new Backbone.Collection([
new Backbone.Model({name:"Charles"}),
new Backbone.Model({name:"Eve"})
]);

var parent = new Backbone.Model({
name:"Bob", children:children
});

var subFactory = function (model) {
var subVm = new kb.ViewModel(model);
subVm.cid = kb.ko.computed(function () {
return model.cid;
});
return subVm;
};

var vm = new kb.ViewModel(null, {
excludes : ["children"]
});

// Do other stuff here that has to be done
// before I add the factory.

vm.shareOptions().factory.
addPathMapping("children.models", subFactory);
vm.createObservables(null, ["children"]);

vm.model(parent);

console.log(vm.name());
console.log(vm.children()[0].cid());
```

This is obviously a strange example since I have the model and could just pass it instead of using `null` but in the real application it's not as easy and this is the smallest I could make the example.

In 0.19.1 I had to change the last part as per you suggestions in #82 to look like this:

```javascript
...

var vm = new kb.ViewModel(null, {
excludes : ["children"],
factories: {"children.models": subFactory}
});

vm.children = kb.observable(null, "children", vm.shareOptions());

...
```

This will fail but if I pass the model directly instead of passing null and later setting the model it works fine.

I've tried really hard to find some kind of workaround for this but I've failed hard so any help would be hugely appreciated.

Thanks.

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the supplied JavaScript example with a ViewModel initialized with null, then inspect ViewModel, createObservables, observable, and the factory path mapping used before model assignment. Done means observables created while the model is null update correctly after vm.model(parent), matching the behavior when the model is passed initially.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.