question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

createViewModel doesn't respect { deep: false }

See original GitHub issue

It happens with mobx-utils 5.1.0:

const { observable } = require('mobx')
const { createViewModel } = require('mobx-utils')
const model = observable.object({ unit: null }, {}, { deep: false })
model.unit = { foo: [1,2,3] }
Array.isArray(model.unit.foo)
// => true
model.unit
// => { foo: [ 1, 2, 3 ] }
Array.isArray(view.unit.foo)
// => true
view.unit = { foo: [1,2,3] }
const view = createViewModel(model)
Array.isArray(view.unit.foo)
// => false
view.unit
// => { foo: [Getter/Setter] }
view.unit.foo
// => ObservableArray [Array] {}

It happens that I pass model to library that tries to do isArray check on some internal array of unit which mobx converted to ObservableArray. I think createViewModel could preserve shallowness to prevent such errors.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
urugatorcommented, Mar 22, 2019

Hacky inefficient solution:

function isShallow(thing) {
  if (isObservableObject(thing)) {
    return !isObservable(thing.$mobx.defaultEnhancer([]));
  }
  if (isObservableArray(thing)) {
    return !isObservable(thing.$mobx.enhancer([]));
  }
  if (isObservableMap(thing)) {
    return !isObservable(thing.enhancer([]));
  } 
  return false;  //?
}

[
  // deep
  observable({}),
  observable([]),
  observable(new Map()),
  // shallow
  observable({}, {}, { deep: false }),
  observable([], { deep: false }),
  observable(new Map(), { deep: false })
].forEach(thing => console.log(isShallow(thing)));
0reactions
mweststratecommented, Oct 15, 2019

Closing for inactivity

Read more comments on GitHub >

github_iconTop Results From Across the Web

MVVM in WPF - How to alert ViewModel of changes in Model ...
I am programming BlackJack. I have a View that doesn't have any code behind and just relies on binding to properties and commands...
Read more >
Design Patterns - Problems and Solutions with Model-View ...
The Model-View-ViewModel (MVVM) design pattern describes a popular ... If the model doesn't have such a property, then the method fails by returning...
Read more >
View Model Doesn't Have To Depend on ViewModel | - Medium
Basically, when the screen (fragment/activity) with this VM is closed for good. Drawbacks: ViewModel should pass through ViewModelProvider.
Read more >
MVVM vs Jetpack ViewModel - what am i doing wrong - Reddit
In that definition, Jetpack ViewModel is actually consolidating the Model side of things for any kind of MV* architecture design. Yes. somehow, ...
Read more >
Setting up a basic MVVM architecture in .NET MAUI
So the idea is to set up my own little 'MVVM framework' with the following ... But in our 'framework', the ViewModel doesn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found