2012-03-03 10 views
1

json配列からの配列でチェックボックスを埋めようとしています。私のコードは、問題は、それがなければならないであろうところの配列が作成され、私は必要なものを持っているが、そのではないということです、バックボーンフォームのチェックボックスの記入方法

var cmyBox = Backbone.Collection.extend({ 
    model: mmyBox, 
    url: 'http://localhost/wordpress/oferta/prueba1/?json=get_taxonomy&taxonomy=habilidad', 
    parse: function (resp) { 
     var skillsname = new Array(); 
     var i = 0; 
     resp.terms.forEach(function(item) { 
      skillsname[i] = item.slug 
      i++ 
     }); 
     console.log(skillsname); 
     return skillsname; 
    } 
}); 

var Form = Backbone.Model.extend({ 
    schema: { 
     id:      {}, 
     nombre:     {}, 
     apellidos:    {}, 
     email:     { type: 'Text', dataType: 'email', validators: ['required', validateEmail] }, 
     telefono:    { type: 'Text', dataType: 'tel', validators: ['required'] }, 
     nacionalidad:   { type: 'Select', options: ['Española', 'Extranjera'] }, 
     link1:     { type: 'Text', title: 'Enlace a Reel', dataType: 'url' }, 
     link2:     { type: 'Text', title: 'Enlace a Web/Blog', dataType: 'url' }, 
     others:     { type: 'Text', dataType: 'url' }, 
     skills1:    { type: 'Checkboxes', options: cmyBoxes }, 
    } 
}); 
var List = new Form ({ 
    skills1: true 
}); 

簡単です。私は収集の結果をそのフィールドに割り当てることを知らない。たぶん私は何か間違っている。私がやりたいWhtatこのです:

skills1:    { type: 'Checkboxes', options: ["artist", "medico", "programador"] }, 

おかげで

+0

もう一度!配列をどこに作成していますか? ( 'cmyBoxes'はどこから来ますか?) –

答えて

2

まあ、私はそれを必要とする誰かのために、解決策を見つけました。非常に便利なtoString関数を使用してください。

var mmyBox = Backbone.Model.extend({ 
     toString: function() { return this.get('slug'); } 
    }); 
    var cmyBox = Backbone.Collection.extend({ 
     model: mmyBox, 
     url: 'http://localhost/wordpress/oferta/prueba1/?json=get_taxonomy&taxonomy=habilidad', 
     parse: function (resp) { 
      return _.map(resp.terms, function(item){ 
       return { 
        id: item.id, 
        slug: item.slug 
       }; 
      }); 
     } 
    }); 
関連する問題