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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:29:30+00:00 2026-06-09T01:29:30+00:00

I have a item model and each item can have many items. This can

  • 0

I have a item model and each item can have many items. This can be many levels deep.

I have this jsfiddle http://jsfiddle.net/rmossuk/xmcBf/3/

how do I change this to be able to have multiple levels of items and also how do I loop down each level to display them ?

I also need be able to order each items children and store that order to persist to server.

please can anyone show me how you can do this ?

UPDATE:

I have now managed to implement the Items can be many levels deep bit of this problem see here http://jsfiddle.net/rmossuk/fBmmS/3/

But i need a way to state the order of each items children and to display them in that order. At the moment it just displays the children items from the itemIds array but this is only used for association purposes and i cant change this to re-order can i ??

Anyone know how to do this ?

Thanks a lot
rick

  • 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-09T01:29:32+00:00Added an answer on June 9, 2026 at 1:29 am

    In the views, use a sortedItems computed property, defined as follow:.

    JS

    App.Item = DS.Model.extend({
      name: DS.attr('string'),
      index: DS.attr('number'),
      items: DS.hasMany('App.Item', {key: 'itemIds'} ),
      item: DS.belongsTo('App.Item'),
      product: DS.belongsTo('App.Product'),
    
      sortedItems: function () {
        var items = this.get('items').toArray();
        return items.sort(function (lhs, rhs) {
          return lhs.get('index') - rhs.get('index');
        });
      }.property('items.@each.isLoaded')
    });
    

    See a full working solution here: http://jsfiddle.net/MikeAski/K286Q/3/


    EDIT

    According to your request, here is another solution, keeping the sorted ids inside the parent (to minimize updates & indexes management): http://jsfiddle.net/MikeAski/K286Q/12/

    JS

    App.Item = DS.Model.extend({
      name: DS.attr('string'),
      items: DS.hasMany('App.Item', {key: 'itemIds'} ),
      sortedIds: DS.attr('string', { key: 'sortedIds' }),
      item: DS.belongsTo('App.Item'),
      product: DS.belongsTo('App.Product'),
    
      sortedChildren: function () {
        if (!this.get('isLoaded')) {
          return [];
        }
        var sortedIds = this.get('sortedIds').split(','),
            items = this.get('items').toArray();
        return sortedIds.map(function (id) {
          if (id === '') {
            return null;
          }
          return items.find(function (item) {
              return item.get('id') == id;
          });
        }).filter(function(item) {
          return !!item;
        });
      }.property('isLoaded', 'sortedIds', 'items.@each.isLoaded')
    });
    

    A little bit more complicated, nevertheless…

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

Sidebar

Related Questions

I have this model var Item = Backbone.Model.extend({ url: 'http://localhost/InterprisePOS/Product/loaditembycategory/Event Materials' }); var onSuccess
Model Item belongs_to User . In my controller I have code like this: @items
I have a Person model & an Item model. A person has many items,
I have a trouble trying to get this working. I have an Item model,
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
I have many items and many users in a database. A user can set
I have an application where I can list Items and add tags to each
I have that situation: I have models Item, Region and Country. class Item(models.Model): name
I have a model item public class EntryInputModel { ... [Required(ErrorMessage = Description is
I have a Model with a Foreign Key of Parent class Item(models.Model): parent =

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.