backbone.stickit icon indicating copy to clipboard operation
backbone.stickit copied to clipboard

bind the model with backbone variable

Open kdbusiness90 opened this issue 10 years ago • 3 comments

Hi All,

How do i bind the model with backbone variable like

var RepeatModal = Backbone.Model.extend({
     defaults: {
        "uRepeateverybyweek": true,
        "uRepeateverybyday": false,
        "uRepeaton": true,
        "uRepeatby": false
     }
 });

In Html:

 <% if(elements.attributes.uRepeateverybyweek == true) { %>
                <select id="uRepeateverybyweek" class="form-control">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                </select>
 <% } %>

elements.attributes.uRepeateverybyweek is not working ? with backbone.stickit

kdbusiness90 avatar Dec 26 '15 12:12 kdbusiness90

You would rather control this in your View. Something like:

bindings: {
    '#uRepeateverybyweek': {
        observe: 'uRepeateverybyweek',
        onGet: function(val) {
            var displayMode = val ? 'inline-block' : 'none';

            this.$('#uRepeateverybyweek').css('display', displayMode);
        }
    }
}

misantronic avatar Dec 26 '15 16:12 misantronic

Hi David,

i have tried above code with below HTML but is giving me.

weeks

Resultant:

true

On Sat, Dec 26, 2015 at 9:45 PM, David Skx [email protected] wrote:

You would rather control this in your View. Something like:

bindings: { '#uRepeateverybyweek': { observe: 'uRepeateverybyweek', onGet: function(val) { var displayMode = val ? 'inline-block' : 'none';

        this.$('#uRepeateverybyweek').css('display', displayMode);
    }
}

}

— Reply to this email directly or view it on GitHub https://github.com/NYTimes/backbone.stickit/issues/301#issuecomment-167338589 .

Kartik*Python | Django | AngularJS Developer * [image: Skype] kdbusiness90 [email protected] | [email protected]

kdbusiness90 avatar Dec 28 '15 18:12 kdbusiness90

@kdbusiness90 you can remove your if statement. Also a small note, this is highly dependent on how you supply your model to your template. Marionette standardizes this heavily, and underscore templates really don't scale as well as something like Handlebars. All that being said, I wouldn't pass your entire model instance into the view, rather a serialized representation. Also: stickit has built-in capabilities for hiding / showing elements based on model attributes.

var MyView = Backbone.View.extend({
  template: _.template('#my_template'),
  render(){
    this.$el.html(this.template(this.model.toJSON())); // Only pass in serialized Model.
    return this;
  },
  bindings: {
    '#uRepeateverybyweek': {
      observe: 'uRepeateverybyweek',
      update: false, // Don't try to update this element, only make it
      visible(val, options){ return val } // visible depending on `uRepeateverybyweek` attribute
    }
  }
});

StevenLangbroek avatar Jan 05 '16 10:01 StevenLangbroek