Collection

collection์€ model๋“ค์„ ๋ชจ์•„ ๋†“์€ ๊ฒƒ์ด๋‹ค. Backbone Model์€ DB์—์„œ ๋ ˆ์ฝ”๋“œ ํ•˜๋‚˜์™€ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๊ณ , Collection์€ DB ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋กœ ๋ฐ›์€ ๋ ˆ์ฝ”๋“œ๋“ค ์ด๋ผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด์„œ Collection์„ ์ƒ์„ฑํ•˜๊ณ  model๋“ค์„ ๋„ฃ๋Š” ๊ฒƒ์„ ๋ณผ ๊ฒƒ์ด๋‹ค.

var Book = Backbone.Model.extend({
    defaults: {
    id: '',
        sort: 'default',
        title: 'default Title'
    }
});

var book1 = new Book({id: 0, sort: 'JavaScript', title: 'JavaScript Book 1'}),
        book2 = new Book({id: 1, sort: 'JavaScript', title: 'JavaScript Book 2'}),
        book3 = new Book({id: 2, sort: 'JavaScript', title: 'JavaScript Book 3'});

// Books ๋ผ๋Š” collection ์ƒ์„ฑ
var Books = Backbone.Collection.extend({        
    model: Book
});

var books = new Books();    

// add ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ model๋“ค์„ books์— ์ถ”๊ฐ€
books.add([book1, book2, book3]);    

// ์ƒ์„ฑ์‹œ์— model๋“ค books์— ์ถ”๊ฐ€
var books2 = new Books([book1, book2, book3]);

Model ์ฐธ์กฐ

Collection์— ๋“ค์–ด์žˆ๋Š” ๋ชจ๋ธ๋“ค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  • collection.models property์— ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์žˆ๋‹ค.

books.models[0];    //=> book1
books.models[1];    //=> book2
books.models[2];    //=> book3
  • .at() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

books.at(0);    //=> book1
books.at(1);    //=> book2
books.at(2);    //=> book3
  • .get(id) : id attribute๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•˜๊ธฐ

books.get(0);    //=> book1
books.get(1);    //=> book2
books.get(2);    //=> book3
  • getByCid() : cid๋กœ ์ ‘๊ทผํ•˜๊ธฐ

books.getByCid('c0');    //=> book1
books.getByCid('c1');    //=> book2
books.getByCid('c2');    //=> book3

Collection Events

s.listenTo ( s.collection, 'add', s.render );
s.listenTo ( s.collection, 'change:orderQty', s.selectedItemChange );
s.listenTo ( s.collection, 'remove', s.selectedItemChange );
  • add : ๋ชจ๋ธ์ด ์ฝœ๋ ‰์…˜์— ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ(collection.add(model)์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

  • remove : ๋ชจ๋ธ์ด ์ฝœ๋ ‰์…˜์—์„œ ์ œ๊ฑฐ๋˜์—ˆ์„ ๋•Œ (collection.remove(model)์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

  • reset : ์ฝœ๋ ‰์…˜์ด ์ดˆ๊ธฐํ™”๋˜์—ˆ์„ ๋•Œ (collection.reset()์„ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

  • sort : ์ฝœ๋ ‰์…˜์ด ์–ด๋– ํ•œ comparator์— ์˜ํ•ด ์ •๋ ฌ๋˜์—ˆ์„ ๋•Œ (collection.sort()๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

  • change : ๋ชจ๋ธ์˜ ์–ด๋– ํ•œ ๊ฐ’์ด ๋ณ€ํ™”๋˜์—ˆ์„ ๋•Œ (model.set(attr: value)๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

  • change:[attr] : ๋ชจ๋ธ์˜ ํŠน์ •ํ•œ ๊ฐ’์ด ๋ณ€ํ™”๋˜์—ˆ์„ ๋•Œ

  • destroy : ๋ชจ๋ธ์ด ์‚ญ์ œ๋˜์—ˆ์„๋•Œ (model.destroy()๋ฅผ ์‹คํ–‰ํ–ˆ์„ ๋•Œ)

fetch

์ด ๋ฉ”์†Œ๋“œ๋Š” ์„œ๋ฒ„์˜ DB๋‚˜ ๋ธŒ๋ผ์šฐ์ €์˜ localStorage์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์™€์„œ ๋ฐ”๋กœ collection์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋•Œ๋Š” collection์„ ๋งŒ๋“ค๋•Œ model property๊ฐ€ ์žˆ์–ด๋„, ์—†์–ด๋„ ๋œ๋‹ค. url property๋Š” ๋ฐ˜๋“œ์‹œ ์žˆ์–ด์•ผํ•œ๋‹ค.

model, collection์— ์ง€์ •๋œ url์— http ํ˜ธ์ถœ์„ ํ†ตํ•ด์„œ JSONํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์—์„œ ๊ฐ€์ ธ์˜จ๋‹ค.

fetch() ๋Š” ์„œ๋ฒ„์—์„œ ์ฃผ๋Š” ๋ฐ์ดํ„ฐ ๋“ค๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ collection์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

fetch()๋ฉ”์†Œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋ฉด url property์— ์žˆ๋Š” url๋กœ ajax request๋ฅผ ํ•œ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋“ค์ด collection์— ๋“ค์–ด๊ฐ„๋‹ค.

var bookModel = new BookModel();
bookModel.fetch({data: })

fetchํ˜ธ์ถœ์‹œ $.Deffered (Promise) ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ด

fetchํ˜ธ์ถœ์‹œ ๊ธฐ๋ณธ์ ์ธ response์˜ ์œ ํšจ์„ฑ๊ฒ€์ฆ (isValidateResponse ํ•จ์ˆ˜) - ์‹คํŒจ์‹œ error์ด๋ฒคํŠธ๋ฐœ์ƒ

Event Bind / Unbind

On

์˜ˆ๋ฅผ ๋“ค์–ด movie๋ผ๋Š” ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค์— change ์ด๋ฒคํŠธ์™€ handler๋ฅผ ๋ฐ”์ธ๋“œํ•œ๋‹ค๋ฉด

movie.on("change",moiveChangeHandler);

๊ฐ„๋‹จํ•˜๊ฒŒ ์ด๋ฒคํŠธ ๋งตํ•‘์„ ์ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

movie.on({
    "change:title": titleChangeHandler,
    "change:actor": actorChangeHandler
})

๋˜ํ•œ Custom ์ด๋ฒคํŠธ๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

movie.on("selected", movieSelectHandler);
movie.trigger("selected");

์ปค์Šคํ…€ ์ด๋ฒคํŠธ๋“ค์€ ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ์ด๋ฒคํŠธ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผํ•˜๋ฉฐ, trigger(event) ใ…ใ…”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด๋œ๋‹ค.

์ฆ‰, trigger ๋ฉ”์†Œ๋“œ๋Š” built-in ์ด๋ฒคํŠธ๋‚˜ ์ง์ ‘ ๋งŒ๋“  ์ด๋ฒคํŠธ๋“ฑ์„ ์ง์ ‘ ํ˜ธ์ถœํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

off

์ด๋ฒคํŠธ unbinding์€ off() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด๋œ๋‹ค.

//ํŠน์ • ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ํŠน์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ
movie.off("change:title", titleChangeHandler);

//ํŠน์ • ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ
movie.off("change:title");

//๋ชจ๋“  ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ํŠน์ • ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ
movie.off(null, eventHandler);

//๋ชจ๋ธ์— ๋Œ€ํ•œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ์ด๋ฒคํŠธํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ
movie.off();

once

ํ•ด๋‹น ์ด๋ฒคํŠธ์— ๋Œ€ํ•ด ํ•œ๋ฒˆ๋งŒ ์‚ฌ์šฉ๋˜๊ณ  ๋ฐ”๋กœ ์ œ๊ฑฐ๋˜๋Š” ์—ญํ• 

(jQuery์˜ one() ๋ฉ”์†Œ๋“œ์™€ ์œ ์‚ฌํ•˜๋‹ค.)

listenTo

์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์€ ๋ชจ๋‘ model, collection์— ์ง์ ‘ ์‚ฌ์šฉ๋œ๋‹ค. Model, Collectiondl ์•„๋‹Œ Backbone.View ์ธ์Šคํ„ด์Šค์— ๊ธฐ๋ฐ˜ํ•œ event binding์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

//movie ๋ชจ๋ธ์— 'title' ์†์„ฑ์ด ๋ฐ”๋€Œ๊ฒŒ ๋˜๋ฉด, view ์ธ์Šคํ„ด์Šค์˜ 'render' ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๋ผ
view.listenTo(movie, "change:title", view.render);

์ฐธ์กฐ๋งํฌ

http://webframeworks.kr/tutorials/backbonejs/backbone_events/

http://codefactory.kr/2011/12/25/getting-started-with-backbonejs-3-collection/

Last updated