AmpersandJS / ampersand-model

Observable objects, for managing state in applications.
MIT License
84 stars 31 forks source link

Props array type doesn't update until after .save() #60

Closed damionvega closed 9 years ago

damionvega commented 9 years ago

I'm unsure if I'm supposed to notify the collection of a change?

The tags property is just an array of strings.

export default Model.extend({
  props: {
    _id:  'string',
    name: 'string',
    tags: 'array',
  },
})

Method in a React Class:


addTag(e) {
  e.preventDefault()
  if (this.state.tagInput.trim() == '') return

  var cat = this.props.cat

  console.log(cat.tags) // food, shopping
  if (!cat.tags) cat.tags = []
  cat.tags.push(this.state.tagInput)
  console.log(cat.tags) // food, shopping, entertainment (not updated in UI)

  this.setState({ submitted: true })

  var cat  = this.props.cat
  var self = this
  cat.save({ tags: cat.tags }, {
    success(model, res, opts) {
      self.setState({
        tagInput: '',
        submitted: false,
      })
      // Tag now displayed in UI
    },
    error(model, res, opts) {
      if (res.success === false) return alert(res.error)
    }
  })
},
damionvega commented 9 years ago

Resolved by adding idAttribute: '_id' to the model. Related: https://github.com/AmpersandJS/ampersand-model/issues/61

wraithgar commented 9 years ago

I'm so glad you got this working!

damionvega commented 9 years ago

I likely would have never figured it out without your help, sir. Thanks again.