dalgard / meteor-viewmodel

Minimalist VM for Meteor
24 stars 2 forks source link

Omit ViewModel properties #11

Closed zimt28 closed 8 years ago

zimt28 commented 8 years ago

I have a form with a lot of fields, every field is bound to a ViewModel property. It looks like I can just omit defining those properties in my ViewModel, which saves me a lot of field: '' lines.

However, it also causes a problem:


template.vm 'users.create',
  firstName: ''
  lastName: ''
  phoneNumber: ''

  _getInvalidKeys: ->
    optionalFields = ['phoneNumber']

    input = _.omit @serialize(), optionalFields

    _.reduce _.keys(input), ((invalidKeys, currentKey) ->
      invalidKeys.push currentKey  unless !!input[currentKey]
      invalidKeys
    ), []

  _isFormValid: ->
    @_getInvalidKeys().length is 0

  _submitForm: ->
    console.log @_isFormValid()
    return  unless @_isFormValid()
<template>
  {{#if _isFormValid}}
    Form is valid!
  {{/if}}
</template>

My _submitForm function always logs the correct value, but the _isFormValid function doesn't work as a template helper anymore when I comment out the properties at the top. Is this behavior intended?

dalgard commented 8 years ago

I think you've hit upon the big gotcha when not declaring a viewmodel or certain properties explicitly, namely that a property cannot be used reliably as a helper before each of its dependencies have been bound via {{bind}}.

The reason is that when _isFormValid is run as a helper the first time, there are no existing properties to register as dependencies for the computation, so it won't rerun reactively at a later time.

As the documentation says, you can either make sure you're only using _isFormValid after all of the {{bind}} statements that create the viewmodel fields it depends on, or you can declare the properties explicitly.

dalgard commented 8 years ago

I gave it another shot, overcoming this limitation of ViewModel, and I'm happy to say that I've succeeded. I'm pushing a new release later today.

dalgard commented 8 years ago

I've released 0.8.0 – please try this version and see if it solves your problem.

zimt28 commented 8 years ago

Hm, the helper still doesn't work /:

dalgard commented 8 years ago

Is it the same if you use the helper after the {{bind}} statement in the template?

zimt28 commented 8 years ago

No, that works. But it dosen't fit my UI

dalgard commented 8 years ago

That's strange. And you're sure you are on 0.8.0? And the bind and helper are in the same template? Does it work if you declare the property explicitly in the viewmodel definition?

dalgard commented 8 years ago

I found a bug in this feature – it doesn't work the second time the same template is used. There should be an exception in the console, have you noticed it?

The new version 0.8.1 fixes this bug. Please try it, if you haven't abandoned this package yet. Cheers.

zimt28 commented 8 years ago

There was nothing logged to the console, but I'll just try 0.8.1 and let you know how things look :)

dalgard commented 8 years ago

Now 0.8.2.

EDIT: Now 0.8.3.

zimt28 commented 8 years ago

Works now :+1:

dalgard commented 8 years ago

Great – thanks a lot for following up on this.