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

bind the model with backbone variable #301

Open
kdbusiness90 opened this issue Dec 26, 2015 · 3 comments
Open

bind the model with backbone variable #301

kdbusiness90 opened this issue Dec 26, 2015 · 3 comments

Comments

@kdbusiness90
Copy link

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

@misantronic
Copy link

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);
        }
    }
}

@kdbusiness90
Copy link
Author

Hi David,

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

Repeat every:
1 2 3 4 5 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
#301 (comment)
.

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

@StevenLangbroek
Copy link

@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
    }
  }
});

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

No branches or pull requests

3 participants