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

When viewOrForm.$el is undefined, use $.find, not $.children? #104

Closed
david-davidson opened this issue Apr 9, 2015 · 6 comments
Closed

Comments

@david-davidson
Copy link
Contributor

Hi there,

I just ran into trouble with form serialization in a Marionette app. Turns out my problem involved Syphon's getForm function--specifically, the fact that it uses $.children, not $.find, to look for form inputs when passed a specific form, not a view.

My use case: I was passing a form to Syphon using Syphon.serialize(this.$('#my-form-id')[0]). (My view was a CompositeView whose $el included other forms [introduced by child views], so .serialize(this) would be too broad.) The form in question looked roughly like this:

<form id="my-form-id">
  <div class="row">
    <div class="form-group">
      <label for="name">Thing name</label>
      <input class="form-control" name="name"></input>
    </div>
  </div>
  <div class="row">
    <div class="form-group">
      <label for="info">Thing description</label>
      <textarea class="form-control" name="info"></textarea>
    </div>
  </div>
  <button type="submit" class="btn disabled">Save</button>
</form>

...that is, with multiple levels between the outer <form> tags and the inputs that I care about.

Within Syphon, in getForm, _.isUndefined(viewOrForm.$el) returned true, since the form itself doesn't have an $el. That meant getForm returned $(viewOrForm).children(':input'). But since $.children only looks one level deep, Syphon didn't find any matching elements and didn't extract any data.

So: since Backbone.View.$, which you use to handle views, uses $.find, and since $.find would find everything $.children does, why not go with $.find here, like this? master...david-davidson:use-jquery-find

Thanks for your time, and for your tremendously useful product!

@rhubarbselleven
Copy link
Contributor

Hi @david-davidson which version are you using?

@lysyiandrii
Copy link

Hi @rhubarbselleven, I agreed with @david-davidson. It would be great if getForm will use jquery find method instead children.

@david-davidson
Copy link
Contributor Author

@rhubarbselleven, sorry for the delay! I'm on backbone.marionette: 2.4.1 and backbone.syphon: 0.6.0.

@rhubarbselleven
Copy link
Contributor

Coolies, anyone interested in a quick PR?

@david-davidson
Copy link
Contributor Author

Will do!

@rhubarbselleven
Copy link
Contributor

Thanks for your perseverance on this one @david-davidson

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