2013-08-24 25 views
1

我試圖完成使用Ember的簡單任務。我想刪除一些目錄中的圖像,並讓我的燼寶應用程序顯示這些圖像。我想出了一個我認爲很聰明的解決方案,就是有一個for循環生成一個夾具數據對象,如果它們是按順序編號的,它將對應於目錄中的圖像。Ember.js夾具適配器ID錯誤,使用for循環產生夾具數據

我似乎越來越近,但我得到這個錯誤:

Uncaught Error: the id property must be defined for fixture "{ id: 1, href: \"public/1.jpg\", style: \"top: 0px; left: 0px\" }"

似乎很奇怪,因爲有證據顯示在所摘錄的固定數據了一個ID。這讓我覺得這可能是數據生成方式中的一個問題?以下是完整的代碼,我的工作:

//CONFIG 
var imgsLength = 3 

//JS-PREP 
var imgLinks = []; 

for (i = 0, topvar = 0, left = 0 ; i<imgsLength ; i++, topvar += 10, left += 10) { 
    imgLinks.push('{ id: ' + (i + 1) + ', href: "public/' + (i + 1) + '.jpg", style: "top: ' + topvar + 'px; left: ' + left + 'px" }'); 
} 

//APP 
App = Ember.Application.create({}); 

App.Store = DS.Store.extend({ 
    revision: 12, 
    adapter: DS.FixtureAdapter 
}); 

App.Router.map(function() { 
    this.resource('images', function() { 
    this.resource('image', { path: ':image_id' }); 
    }); 
}); 

App.ImagesRoute = Ember.Route.extend({ 
    model: function() { 
    return App.Image.find(); 
    } 
}); 

var attr = DS.attr; 

App.Image = DS.Model.extend({ 
    href: attr('string'), 
    style: attr('string'), 
}); 

App.Image.FIXTURES = imgLinks; 

及相關HBS代碼:

{{#each model}} 
    {{#linkTo image this}}<div {{bindAttr style="style"}}><img {{bindAttr src="href"}}></div>{{/linkTo}} 
{{/each}} 

的思考?

回答

0

This is making me think maybe it's an issue in the way the data is being generated?

你猜對了這是你生成導致問題的燈具的方式。因此,嘗試做這樣的代替:

for (i = 0, topvar = 0, left = 0 ; i<imgsLength ; i++, topvar += 10, left += 10) { 
    var image = {}; 

    image.id = (i + 1); 
    image.href = "public/" + (i + 1) + ".jpg"; 
    image.style = "top: " + topvar + "px; left: " + left + "px"; 

    imgLinks.push(image); 
} 

你也應該把你的鏈接途徑進入報價:

{{#each model}} 
    {{#linkTo 'image' this}}<div {{bindAttr style="style"}}><img {{bindAttr src="href"}}</div>{{/linkTo}} 
{{/each}} 

看到這裏的工作jsbin

希望它有幫助。

+0

完美。非常感謝。爲什麼定義文字然後將其推送到數組工作,但我的原始嘗試不?只是想更好地理解。 – curtis

+0

@curtis,我想這是因爲你如何構建它被翻譯成字符串而不是對象的對象。 – intuitivepixel