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 8099781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:31:25+00:00 2026-06-05T22:31:25+00:00

I am new to backbone and I am stuck with a problem problem in

  • 0

I am new to backbone and I am stuck with a problem

problem in short

I have two lists to be shown.

1.Courses

2.Students for selected course

which I am able to show now.

Now in the StudentList view(it uses “Students” collection) I have an add button,which adds the student for the selected course item(which is in separate view). For that I need to know which course item I have clicked inside the StudentList view. For that I have stored the courseId in a hidden field when a course is clicked and later in StudentList view I have fetched that hidden field value to add the new student.

What I want to do is instead of storing the courseId in a hidden field,can I add the courseId to the “Students” collection as an attribute when a course is clicked.

Tried Approach

Inside StudentList view I have written something like this

var StudentList = Backbone.View.extend({
 initialize: function () {
    this._meta = {};       
},  

put: function (prop, value) {
    this._meta[prop] = value;
},
get: function (prop) {
    return this._meta[prop];
},   
events: {
    "click    #btnAddStudent": "createNewStudent"
},

createNewStudent: function () {      
    var some =  this.get("someProp");       
    this.collection.create({ Name: this.$el.find('#txtNewStudent').val(), 
    CourseId: some  });       
}
});

And in “courseClicked” function I have done like this(the “courseClicked” function is inside the CourseItem view)

var CourseItem = Backbone.View.extend({  
events: {        
    'click': 'courseClicked'
},
initialize: function () {
    this.students = this.options.students;
},  
courseClicked: function () {
    var courseId = this.model.id;
    this.students.put('someProp',courseId);
    this.students.fetch({ data: { courseId: courseId} });
}   
});

The above doesn’t work because put and get functions are not available in the courseitem view context,they are defined in StudentList view.Can anyone guide me how to accomplish my needs.

This question is continuation to the one I posted earlier on SO here
Using Event Aggregator to load a view with different model in backbone js
This link might help to know further details of my question.

Thanks for your patience

  • 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-05T22:31:27+00:00Added an answer on June 5, 2026 at 10:31 pm

    this should be a partial answer which might lead you to right way or into the trenches. Here it goes.

    1. It is best that you have 2 views one for Students list and one for course list. Providing a checkbox list should be good for the course list as Students -> Course is one to many relationship.

    2. Course(model) should have a selected attribute(only on client, no significance to server side) which you toggle during checkbox click. Courses (collection) should redraw the changed course with highlights etc

    3. The Courses(Collection) should be accessible through window object. something like window.App.Students.Courses where Courses is an instance of Courses collection.
      ex:window.App.Students.Courses = new window.App.Students.Collection.Courses;

    4. Now when the course is selected and students are selected. You have global access to courses that have been selected ( which can be retrieved using underscore.js methods) then set the course id into a array for the selected students. something like for each selectedstudent ( foreach selectedCourse selectedStudent.course.push(selectedCourse)). json for student will then look like below

    {
    id: 1,
    name: "Jack Keane",
    courses: [1, 2, 5, 6, 7]
    }

    Code

    Inside the Student view, do the following to get the selected courses for the student.

    window.courses.filter(function(course){
      return course.get("selected");
    });
    

    use the filter method for obtaining the selected courses. Then assign the course to the student as in #4.

    var selectedCourses =     window.courses.filter(function(course){
          return course.get("selected");
        });
    
    this.collection.forEach(function(student){
    
    student.set("courses") = selectedCourses;
    
    });
    

    by doing the above, every student now has courses attribute that gives you the selected courses for this student.

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

Sidebar

Related Questions

I have a parent backbone view which creates a new backbone view in its
I have created a new backbone.js widget model which I hope to extend: var
I'm rather new to Backbone.js development, and have run into a bit of a
I am new to backbone.js , and have a query. Say my user is
I'm new to Backbone. Is it possible to define a Model in backbone which
I'm new to JS and Backbone What's the difference between these two? TestModel =
I have a backbone view - which when called presents a form $('#add-offer').click(function() {
I'm relativly new to Backbone.js I have a JSON like the picture shows !
I have just started with backbone.js. And I'm having a problem in fetching the
I am new to Backbone.js. I have created a View for a button that

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.