how to make backbone.stickit collect all values at once?

500 views Asked by At

Problem:

I used backbone.stickit to do 2 way binding for the form. however, I found it hard to cancel the collection unless I set updateModel to false for all attributes. so, the issue is, how can i grab all the values from the form when user click save button.

A.K.A how do i manually collect data all at once in stickit?

I tried to access it to get all handlers so I can trigger manually, but apparently, since getConfiguration function is private, i do not have access to it.

2

There are 2 answers

1
user2095627 On BEST ANSWER

Take a look at Backbone.trackit - it was designed to help manage unsaved attributes, and works well with stickit.

0
coderek On

i was wrong. getConfiguration is accessible throught Backbone.Stickit.getConfiguration, so i come up with this function to get all values.

  // for backbone form views with stickit enabled
  // to get all observed fields at once
  var stickitGetValues = function (values) {
    _.each(this.bindings, function(v, selector) {
      var namespace = '.stickit.' + this.model.cid;
      var $el = this.$(selector);
      var binding = this.bindings[selector];

      var config = Backbone.Stickit.getConfiguration($el, binding);

      if (values.indexOf(config.observe) != -1 && config.events && config.events.length > 0) {
        var event = config.events[0] + namespace;
        var val = config.getVal.call(this, $el, event, config);
        this.model.set(config.observe, val, {silent: true});
      }
    }, this);
  }

to use it just call it inside the backbone.view

stickitGetValues.call(this, ["name", "age", "whatever"]);

so the view's model will be updated accordingly.