2012-03-01 33 views
0

我想用請求URL中的JSON數組填充複選框數組。我已經閱讀了文檔,我發現我可以用模型和集合來做到這一點,但我不知道如何開始。如果有人能告訴我路徑是什麼,我將不勝感激。這是我的代碼:用新集合填充複選框數組

var mBox = Backbone.Model.extend({ 

}); 
var cBox = Backbone.Collection.extend({ 
    model: mBox, 
    url: 'http://localhost/oferta/prueba1/?json=get_taxonomy&taxonomy=habilidad&dev=1' 
}); 
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' }, 
     otros:     { type: 'Text', dataType: 'url' }, 
     skills:     { type: 'Checkboxes', options: new cBox() }, 
    } 
}); 

回答

1

你是一個好的開始。我認爲可能會讓你感到沮喪的是數據如何被實際提取並放入收藏中。您在cBox上定義了一個url屬性,您的收藏,但它本身並不實際得到任何數據從服務器。您必須調用Backbone的Collection.fetch()方法來獲取數據並將其放入集合中。

我會做這樣的事情:

cBoxCheckboxes = new cBox(); // create a new cbox, but there is no data in here yet 
cBoxCheckboxes.fetch(); // make a get request to the server (at the url you've specified) to get the data 
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' }, 
     otros:     { type: 'Text', dataType: 'url' }, 
     skills:     { type: 'Checkboxes', options: cBoxCheckboxes /* the collection with the data in it */ }, 
    } 
}); 
+0

是的,這是工作,但它沒有分配結果的選項,我可以看到控制檯的結果,但它不存在......爲什麼? – ki0 2012-03-03 18:19:12

+0

我做了一個解析函數,它返回它的一個數組,它會出現在選項上,但我沒有得到任何東西。謝謝 – ki0 2012-03-03 18:25:08

0

該解決方案不工作的樣子,我想..我重寫代碼和我這樣做:

var mmyBox = Backbone.Model.extend({}); 

var cmyBox = Backbone.Collection.extend({ 
    model: mmyBox, 
    url: 'http://localhost/wordpress/oferta/prueba1/?json=get_taxonomy&taxonomy=habilidad&dev=1', 
    parse: function (resp, xhr) { 
     console.log(resp.terms); 
     return resp.terms; 
    } 
}); 

function validateEmail(str) { 
    var regex = new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"); 

    return regex.test(str) ? null : 'Invalid email'; 
} 

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' }, 
     otros:     { type: 'Text', dataType: 'url' }, 
     skills:     { type: 'Checkboxes', options: new cmyBox() }, 
    } 
}); 

分析函數還給來自json的數組項。

Array 
{ 
    "status": "ok", 
    "count": 3, 
    "terms": [ 
    { 
     "id": 11, 
     "slug": "artist", 
     "title": "artist", 
     "description": "", 
     "post_count": 1 
    }, 
    { 
     "id": 13, 
     "slug": "medico", 
     "title": "medico", 
     "description": "", 
     "post_count": 1 
    }, 
    { 
     "id": 12, 
     "slug": "programador", 
     "title": "programador", 
     "description": "", 
     "post_count": 1 
    } 
    ] 

看來比新的cmyBox沒有創建新的集合。那麼它就是我通過控制檯看到的。

感謝喬希的答案。乾杯。

+0

問題來自url字符串。最後它有一個dev標籤..出於這個原因沒有工作。 – ki0 2012-03-03 16:28:09