๋๋ ๋ฐ๋ฅด์ง ์๋๋ค. ๋ชจ๋ธ์ ์ด๋ป๊ฒ ํจ์๋ก ์ ์๋ฉ๋๊น?
์ค์ ๋ก ์ด์ ์ด ๋ฌธ์ (๋๋ ์ด์ ์ ์ฌํ ๊ฒ)๊ฐ ๋ฐ์ํ์ต๋๋ค.
๋คํ์ฑ ๋ชจ๋ธ ์์ฑ์ ์ํด ์ปฌ๋ ์ ์ ๋ชจ๋ธ ํจ์๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
์ปฌ๋ ์
์์ .add
๋ฅผ ํธ์ถํ ๋ ๋ฐฑ๋ณธ ์ฝ๋๋ model
์์ฑ์์ .prototype.idAttribute
๋ฅผ ํธ์ถํ๋ ค๊ณ ์๋ํ์ง๋ง model
ํจ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ๊ฒฝ์ฐ ES2015 ํด๋์ค ๋ฉ์๋์๋ prototype
์์ฑ์ด ์์ต๋๋ค.
์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ์ญ์์ค: https://github.com/jashkenas/backbone/blob/75e6d0ce6394bd2b809823c7f7dc014ddb6ae287/backbone.js#L1102
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
const Library = Backbone.Collection.extend({
model () {
// stuff goes here
}
});
๋ค์๊ณผ ๊ฐ์ด ์ง์ ํ๋ฉด ๋ฌธ์ ๊ฐ ์ฌ๋ผ์ง๋๋ค.
const Library = Backbone.Collection.extend({
model: function () {
// stuff goes here
}
});
์ค์ ๋ก ์ด์ ์ด ๋ฌธ์ (๋๋ ์ด์ ์ ์ฌํ ๊ฒ)๊ฐ ๋ฐ์ํ์ต๋๋ค.
๋คํ์ฑ ๋ชจ๋ธ ์์ฑ์ ์ํด ์ปฌ๋ ์ ์ ๋ชจ๋ธ ํจ์๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
์ปฌ๋ ์ ์์
.add
๋ฅผ ํธ์ถํ ๋ ๋ฐฑ๋ณธ ์ฝ๋๋model
์์ฑ์์.prototype.idAttribute
๋ฅผ ํธ์ถํ๋ ค๊ณ ์๋ํ์ง๋งmodel
ํจ์๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์ ์ํ ๊ฒฝ์ฐ ES2015 ํด๋์ค ๋ฉ์๋์๋prototype
์์ฑ์ด ์์ต๋๋ค.์ฌ๊ธฐ๋ฅผ ๋ด:
์๋ฅผ ๋ค์ด ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
const Library = Backbone.Collection.extend({ model () { // stuff goes here } });
๋ค์๊ณผ ๊ฐ์ด ์ง์ ํ๋ฉด ๋ฌธ์ ๊ฐ ์ฌ๋ผ์ง๋๋ค.
const Library = Backbone.Collection.extend({ model: function () { // stuff goes here } });
๋ฌธ์ ์ ๋ํ ์์ธํ ์ค๋ช ๊ฐ์ฌํฉ๋๋ค!
๊ทธ๋ฌ๋ ๊ทํ์ ๋๋ต์ idAttribute์ ๋ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒ์ด ์๋๋๋ค.
์ถ๊ฐ ์ ์ ๋ชจ๋ธ ์ฐธ์กฐ๊ฐ ์ถ๊ฐ๋์ง ์์์ต๋๋ค. ๋ฐ๋ผ์ id๋ก ์ ๋ชจ๋ธ์ ์ป์ ์ ์์ต๋๋ค.
Workarond - ๋ชจ๋ธ๊ณผ ๋์ผํ model.prototype.idAttribute๋ฅผ ์ถ๊ฐํ์ต๋๋ค.
๋ฌธ์ ๋ Collection
์ model
$๊ฐ ๋ค์๊ณผ ๊ฐ์ด ์ ์๋๋ค๋ ๊ฒ์
๋๋ค. Override this property to specify the model class that the collection contains.
์ด API๋ ํจ์๋ก ์ ์๋๋ ๋ชจ๋ธ์ ์ง์ํ์ง ์์ต๋๋ค.
@paulfalgout Collection.model์ ๋ํ ๋ฐฑ๋ณธ ๋ฌธ์๋ ํจ์๋ฅผ model
๋ก ์ค์ ํ ์ ์๋ค๊ณ ์ง์ ํฉ๋๋ค. model ()
๊ตฌ๋ฌธ( model: function()
๋์ )์ผ๋ก ์ด๊ฒ์ด ๋ถ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ๋์๊ฒ ๋ฒ๊ทธ์ฒ๋ผ ๋ณด์ด๊ฑฐ๋ ์ ์ด๋ ๊ทน๋๋ก ํผ๋์ค๋ฌ์ ๋ณด์
๋๋ค.
์, ์๋ณธ ๋ฉ์์ง์์ ๋ฌธ์์ ๊ด๋ จ ๋ถ๋ถ์ ์ฐ๊ฒฐํ์ต๋๋ค.
์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ์ญ์์ค: https://backbonejs.org/#Collection -model
@PavelKoroteev : ์ฐ๋ฆฌ์ ๋ฌธ์ ๊ฐ ๊ด๋ จ๋์ด ์๋ ๊ฒ ๊ฐ์ง๋ง ๊ฐ์ง๋ ์์ต๋๋ค.
๋ด๊ฐ ๋งํ๋ ๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค(์ฝ์์์ ๋ถ์ฌ๋ฃ์).
collection = new (Backbone.Collection.extend({ model (attrs, options) { return new (Backbone.Model.extend({ idAttribute: 'customId' }))(attrs, options)}}));
childย {length: 0, models: Array(0), _byId: {โฆ}}
collection.add({ customId: 'id.1' });
backbone.js:1102 Uncaught TypeError: Cannot read property 'idAttribute' of undefined
at child.modelId (backbone.js:1102)
at child.get (backbone.js:997)
at child.set (backbone.js:872)
at child.add (backbone.js:814)
at <anonymous>:1:12
๋ฌธ์ ๋ ์ฌ์ค ๋๊ฐ๋ค๊ณ ์๊ฐํฉ๋๋ค. ๋ํ https://github.com/jashkenas/backbone/issues/4203 ๊ณผ ๋์ผํฉ๋๋ค.
model: function(attrs, options)
๋ฅผ ์ฌ์ฉํ๋ฉด idAttribute
๊ฐ ์๋ ํ๋กํ ํ์
์ด ์์ฑ๋๊ณ id
๋ก ๋์ฒด๋์ด "์๋"ํฉ๋๋ค.
๋คํ์ฑ ๋ชจ๋ธ์ด ๋ฌธ์ํ๋์ด ์์ต๋๋ค. _ํ์ง๋ง_... ๊ฝค ์ค๋ซ๋์ model.prototype
๊ฐ ์๋ ์ด ๋ผ์ธ ๋๋ฌธ์ idAttribute
๋๋ es6 ์ ํ ๋ฉ์๋๋ฅผ ์ง์ํ์ง ์์์ต๋๋ค.
https://github.com/jashkenas/backbone/pull/3966 ์ด ์ผ์ข ์ ์์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค :-/
2016๋ ์ PR์ด ์ ์ฝํด์ง๊ณ ๋ณํฉ๋์ง ์๋ ๊ฒ์ ๋ณด๋ฉด(๋๋ ๋ ๋์ ๊ฒ์ ์ํด ๊ฑฐ๋ถ๋จ) ๊ธฐ๋ณธ์ ์ผ๋ก ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด PR์ ์๋ํ๋ ๊ฒ์กฐ์ฐจ ๊บผ๋ คํฉ๋๋ค.
๋ด๊ฐ ๋งํ ์ ์๋ ๋ฐ์ ๋ฐ๋ฅด๋ฉด Backbone์ ๊ฑฐ์ ์ฃฝ์์ต๋๋ค.
๋ค๋ฅธ ์ฌ๋๋ค์ด ํ๋ ๊ฒ์ฒ๋ผ ๋๋ ํฌํฌ๋ฅผ ๋ง๋ค์์ต๋๋ค. ๋ด ์ด๋ฆ์ Skeletor ์ ๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ์ฌ Backbone ์ฝ๋๋ฅผ ์ต์ API ๋ฐ ํ์ค์ผ๋ก ์ ๋ฐ์ดํธํ ๋ค์ ์ ์ฐจ ๋ ๋์ ๊ฒ์ผ๋ก ์ฎ๊ฒจ๊ฐ ๊ฒ์ ๋๋ค.
์ถฉ๋์ ํด๊ฒฐํ ํ ์ด์ https://github.com/jashkenas/backbone/pull/3966 PR์ ๋ณํฉํ์ต๋๋ค. ๋ฒ๊ฑฐ๋ก์ฐ์๋๋ผ๋ ์๋ ค์ฃผ์ธ์...
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ถฉ๋์ ํด๊ฒฐํ ํ ์ด์ https://github.com/jashkenas/backbone/pull/3966 PR์ ๋ณํฉํ์ต๋๋ค. ๋ฒ๊ฑฐ๋ก์ฐ์๋๋ผ๋ ์๋ ค์ฃผ์ธ์...