how to manage multiple checkboxes in ember?

iguider

I'm still trying to figure out how ember works and I want to have more info on managing multiple checkboxes in ember..
here's what I tried to do: http://jsbin.com/datojebu/2/edit
as you can see all checkboxes get selected and the checked function doesn't get called
what's the correct way of doing this?

Julian Leviston

Okay further to your additional questions, I've basically finished your app for you:

http://jsbin.com/datojebu/11/edit

App = Ember.Application.create();
App.ApplicationAdapter = DS.RESTAdapter.extend({
    host: '/api',
    namespace: 'fr'
});

/* router */
App.Router.map(function() {
  this.resource('animes');
});
App.AnimesRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('anime');
  },
  setupController: function(controller, model) {
    this._super();
    this.store.find('genre').then(function(genres) {
      controller.set('genres', genres);
    });
    controller.set('model', model);
  }
});

/* models */
var model = DS.Model,
    attr = DS.attr,
    hasMany = DS.hasMany;

App.Genre = model.extend({
    animes: hasMany('anime', {async: true}),
    nom: attr('string')
});
App.Anime = model.extend({
    nom: attr('string'),
    parution: attr('number'),
    synopsis: attr('string'),
    likes: attr('number'),
    auteur: attr('string'),

    genres: hasMany('genre', {async: true})
});
/* controllers */
App.AnimesController = Em.ArrayController.extend({
  genres: Em.A([]),
  selectedGenreIds: Em.A([]), // a set of ids
  selectedGenres: function() {
    var genres = this.get('genres'),
        selectedGenreIds = this.get('selectedGenreIds');
    return genres.filter(function(genre) {
      return selectedGenreIds.contains(genre.get('id'));
    });
  }.property('selectedGenreIds.@each', 'genres.@each'),
  selectedAnimes: function() {
    var allAnimes = this.get('model'),
        selectedGenres = this.get('selectedGenres'),
        filteredAnimes;
    // for an anime to be shown, it has to have at
    // least one of its genres selected
    filteredAnimes = allAnimes.filter(function(anime) {
      return anime.get('genres').any(function(animeGenre) {
        return selectedGenres.contains(animeGenre);
      });
    });
    return filteredAnimes;
  }.property('model.@each', 'selectedGenres.@each', 'genres.@each')
});
App.GenreController = Em.ObjectController.extend({
  needs: ['animes'],
  isChecked: function(key, value) {
    if(arguments.length > 1) {
      // setter
      var selectedGenreIds = this.get('controllers.animes.selectedGenreIds'),
          thisId = this.get('id');
      if(!selectedGenreIds.contains(thisId) && value) {
        selectedGenreIds.addObject(thisId);
      } else {
        selectedGenreIds.removeObject(thisId);
      }
    }
    // always return the value for the getter and the setter
      return value;
  }.property('controllers.animes.selectedGenreIds')
});

/* mockjax */
var animes = [
    {
        id: 1,
        nom: 'Blah',
        parution: 2014,
        genres: [1, 3],
        synopsis: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore, eveniet, ab pariatur omnis dolor sunt alias atque voluptate neque reiciendis maiores impedit quibusdam perferendis optio ratione expedita adipisci et. Cupiditate!',
        likes: 206,
        auteur: 'Moi :p'
    }
],
genres = [
    {
        id: 1,
        nom: 'action',
        animes: []
    },
    {
        id: 2,
        nom: 'magie',
        animes: [1]
    },
    {
        id: 3,
        nom: 'amour et amitier',
        animes: []
    },
    {
        id: 4,
        nom: 'aventures',
        animes: [1]
    }
];
$.mockjax({
  url: '/api/fr/animes',
  responseTime: 750,
  responseText: {
    'animes': animes
  }
});
$.mockjax({
  url: '/api/fr/genres',
  responseTime: 750,
  responseText: {
    'genres': genres
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

WIN32 How to make multiple checkboxes and manage them differently?

From Dev

How to manage state with checkboxes in React?

From Dev

Ember - get state of multiple checkboxes one by one

From Dev

Manage Ember Multiple RSVP Promises on the same route

From Dev

How to validate multiple checkboxes

From Dev

How to manage multiple projects

From Dev

How to manage multiple promises

From Dev

Multiple Choice Questionnaire Using Checkboxes and Computed Properties in Ember.js

From Dev

How to check multiple checkboxes with JavaScript?

From Dev

How to use multiple checkboxes in jmeter?

From Dev

How to Show or Hide multiple divs with multiple checkboxes

From Dev

How to manage multiple windows in MVVM

From Dev

How to manage multiple profiles in Jhipster

From Dev

How to manage multiple configuration for vim?

From Dev

How to manage multiple APIs in Rails

From Dev

Python: How to manage multiple user?

From Dev

How to manage multiple AsyncTasks in Android

From Dev

Ember checkboxes are connected

From Dev

Checkboxes in Ember Js

From Dev

How does Ember Data manage large amount of records?

From Dev

Ember how to use multiple adapters?

From Dev

Multiple Checkboxes

From Dev

how to save multiple checkboxes value in wordpress

From Dev

PHP how to multiple rows of form via checkboxes

From Dev

How to create a dialog with checkboxes that allows multiple selections

From Dev

how can I verify if multiple checkboxes are checked

From Dev

How to delete multiple entries using Checkboxes? (PHP)

From Dev

How to style multiple appended checkboxes with labels

From Dev

How to create a dialog with checkboxes that allows multiple selections

Related Related

HotTag

Archive