Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Initialize Change Function #50

Closed
jakesower opened this issue Nov 4, 2016 · 5 comments
Closed

Initialize Change Function #50

jakesower opened this issue Nov 4, 2016 · 5 comments

Comments

@jakesower
Copy link

I would like to run a change function when my object is bound. Example:

let mainNode = bindObject( state, [ mainTemplate, {
  messages: '#messages',
  name: [ '#signin', function ( node, value, previousValue ) {
    console.log('i ran');
    if( value === null ) {
      node.style.display = 'block';
      return bindObject.retainElement;
    }
    else {
      node.style.display = 'none';
    }
  }]
}]);

The change function associated with name doesn't get run unless I actually change state.name. While it is a function called change, it would be nice if it ran at init time. messages is automatically inserted as expected at init.

Is this a misguided approach, and if so, do you have any recommendations on how to get that function to run other than setting state.name after init time?

Thank you.

@gr0uch
Copy link
Owner

gr0uch commented Nov 4, 2016

Quote from the readme:

A change function can be determined to be an insert, mutate, or remove operation based on whether the value or previous value is null:

Value but not previous value: insert operation.
Value and previous value: mutate operation.
No value: remove operation.

You probably don't want the else clause, because that could be either an insert or update. In this case, if you want to check for insertion, it would be:

if (previousValue == null) { ... }

Is calling it the change function misleading? I wasn't too sure about the name, maybe it could be called the mapping function or something, it's only called change internally and in the docs.

@jakesower
Copy link
Author

I apologize for the large amount of time that's passed since I said anything. To answer your question right away, I did not find the name of the change function to be misleading.

I've written a test that shows the error more clearly: https://github.com/jakesower/simulacra/commit/8ad5faefa40392a0833e088b16eea73db477fcde

It appears to be due to having a bound variable set to null at initialization, rather than what I initially suspected (and what you addressed). It's something of an edge case, so I intend to put in a fix and submit it as a PR.

Please let me know if the test is invalid for some reason, otherwise I'll send you a PR that makes the test pass in the coming hours/days.

@gr0uch
Copy link
Owner

gr0uch commented Nov 9, 2016

I think the test case is mistaken here: https://github.com/jakesower/simulacra/commit/8ad5faefa40392a0833e088b16eea73db477fcde#diff-910eb6f57886ca16c136101fb1699231R202

Since the value of change is initialized to null, no element is rendered initially. Consider that in terms of rendering, these states are the same:

{ simple: 'Simple', change: null }
{ simple: 'Simple', change: undefined }
{ simple: 'Simple' }

null and undefined values are considered to be either not renderable, or an intent to remove the corresponding element. This means that the element is non-existent for null values.

In the example on the homepage you can try:

state.details = null

and it will remove the entire element corresponding to the value.

@jakesower
Copy link
Author

I've tried to do something decidedly unusual. The behavior isn't exactly what I expected, but it's logical. I can pass in some other value that means null for purposes of my logic that simulacra won't think of as a null value. Essentially my expectation was that I could "hijack" the change function to override the default way that simulacra treats null.

Thanks for your time! I'll just formulate my code differently to skirt the issue.

@gr0uch
Copy link
Owner

gr0uch commented Nov 14, 2016

That sounds perfectly fine to do actually. I will close this now.

@gr0uch gr0uch closed this as completed Nov 14, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants