Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question about state and adding elements to the collection #19

Open
richard-engineering opened this issue Oct 13, 2016 · 6 comments
Open

Comments

@richard-engineering
Copy link

richard-engineering commented Oct 13, 2016

I have this bit of code based on what I can tell from the TODOMVC is the right way to define state

export default class Entry extends Model {
  default = {
    name: 'test_name'
  }
}
export default class Home extends Component {

  constructor(props) {  // componentWillMount
    super(props);
  }

  state = {
    entries: Entry.Collection,
  }

In the console when debugging (eg in render or in constructor) I notice this.state.entries.add does not exist. this.state.entries.create().add is defined. What would be the proper way to add something to the entries collection? Should I declare

state = {
  entries: Entry.Collection.create()
} 

instead?

@richard-engineering richard-engineering changed the title Does this work with classes or should I use the model.export and React.createClass? Question about state and .add Oct 14, 2016
@richard-engineering richard-engineering changed the title Question about state and .add Question about state and adding elements to the collection Oct 14, 2016
@richard-engineering
Copy link
Author

richard-engineering commented Oct 14, 2016

Also, adding on top of your answer in VoliJS/NestedLink#12, state.getLink is undefined for me. I assume I need to import valuelink and use valuelink like normal instead? The documentation on this page seems to imply that I get it for free from nestedtypes.

render() {
  const { state } = this;
  const entriesLink = state.getLink( 'entries' );
  return (
    ....
    {
      entriesLink.map((entryLink, key) => <li key={key}>{entryLink.value.name}</li>)
    }
}

Edit: The following is defined, but it doesn't return anything even though I added an entry to the collection? I think the documentation on the front page needs to be updated.

state.entries.getLink()

@richard-engineering
Copy link
Author

I also tried not using classes with React.createClass and Model.extend per the examples and I get
Uncaught TypeError: this.defaults is not a function in the console. I am compiling to electron via webpack, maybe something is weird with my setup.

'use strict';

import React from 'nestedreact';
import { Entry } from '../models/Entry.js';

export const Home = React.createClass({

  state: {
    entries: Entry.Collection
  },

  render() {
    return (
      <div>
        <div>
          <h2>HOME TEST</h2>
        </div>
      </div>
    );
  }
});
'use strict';

import React from 'nestedreact';
import { Model } from 'nestedtypes';

export const Entry = Model.extend({

  defaults: {
    name: "default_name"
  }
});

@gaperton
Copy link

gaperton commented Oct 18, 2016

You forgot @define decorator. Must be imported either from nestedreact or nestedtypes. Must precede the class definition of the Component and the Model.

@define is the place where all the magic happens, it performs meta-transformations crucial for the things to work. If you can't or don't want to use decorators, just call YouClass.define() right after the definition. That's what this decorator does.

@gaperton
Copy link

.createClass and .extend work without @define, of course. I will check your example locally.

@gaperton
Copy link

Your last example works perfectly in Chrome. Try to take any of the examples from examples folder (except flux_comparison) as starting boilerplate for your test.

@richard-engineering
Copy link
Author

Thank you for the replies, I didn't even know that decorators existed in javascript, hahah. I will try this when I have the time and let you know, we are quite busy at work right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants