Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7866457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:24:28+00:00 2026-06-03T00:24:28+00:00

If I am using Backbone, which (or both) of these is the correct way

  • 0

If I am using Backbone, which (or both) of these is the “correct” way to set data?

// Logic in the Model
// Can call from anywhere that has access
SomeModel = Backbone.Model.extend({
  defaults: {
    visible: false
  },

  toggle: function(visible){
    visible = typeof visible !== "undefined" ? visible : !this.get("visible");
    this.set({visible: visible});
  }
});

OR

// Logic in the View
SomeView = Backbone.View.extend({
  events: {
    "click .toggle" : "toggleVisibility"
  },

  toggleVisibility: function(){
    this.model.set({visible: !this.model.get("visible")});
  }
});

Obviously either one of these work, but my question is how much of that logic should be pushed off to the model? If, for instance, I have a situation that updates two variables:

this.model.set({visible: false, foo: bar, something: else});

Would it make sense to create a function on the model like this:

someFunction: function(visible, foo, something){
  this.set({visible: visible, foo: foo, something: something});
}

That just seems like overkill to me, but the set({}) logic in the view feels dirty.

Thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:24:30+00:00Added an answer on June 3, 2026 at 12:24 am

    It seems that you’re mixing view logic into your models which isn’t really a great idea. I would imagine there is some data in your model that is relevant to whether the view that has it bound should be visible (something like deleted: true, etc.) but you should toggle visibility based on that attribute changing. I would envision something like:

    SomeModel = Backbone.Model.extend();
    
    SomeView = Backbone.View.extend({
      events: {
        "click .toggle" : "toggleDeleted"
      },
    
      initialize: function() {
          this.model.on('change:deleted', this.toggleVisibility);
      },
    
      toggleDeleted: function() {
        var deleted = this.model.get('deleted');
        this.model.set({deleted: !deleted});
      },
    
      toggleVisibility: function(){
        this.$el.toggle(); // jQuery function toggling visibility
      }
    });
    
    // Somewhere in your "controller" logic
    var someModel = new SomeModel({deleted: false});
    var someView = new SomeView({model: someModel, el: DOMelement});
    

    That way all your view logic is in your view and not your model. The model is empty since backbone has no need to define a schema, but generally you’ll add functionality there at some point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the backbone-boilerplate, which you can find here . I was wondering
NOTE: This question is related to CodeIgniter-RestServer When I call model.save() from backbone the
I am using Backbone.js to create compound form fields which hold a telephone number
I'm developing application using backbone.js & jquery. I have following code in model: runReport:
I'm using backbone with the backbone-rails gem which does its own templating and project
I am using backbone.js on a rails backend with HAML Coffee , which is
I'm using backbone, and the general way for passing the collections when the page
I'm using backbone.js, underscore.js and jQuery. I took the following sample code from http://jsfiddle.net/thomas/C9wew/6/
I'm using Backbone.js have a segmented control-type UI element for each model's view. They
(Rails 3 + backbone 0.53) I am fetching data from my API on /payments

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.