Backbone: Backbone.Model에 "reset"λ©”μ†Œλ“œ μΆ”κ°€

에 λ§Œλ“  2014λ…„ 07μ›” 31일  Β·  22μ½”λ©˜νŠΈ  Β·  좜처: jashkenas/backbone

μ„œλ²„μ—μ„œλ°›μ€ λͺ¨λΈ 데이터 쀑 일뢀λ₯Ό μ—…λ°μ΄νŠΈν•΄μ•Όν–ˆμŠ΅λ‹ˆλ‹€. ν˜„μž¬ 두 가지 μ˜΅μ…˜μ΄ μžˆμŠ΅λ‹ˆλ‹€. Model.set ν˜ΈμΆœν•˜κ±°λ‚˜ Model.attributes 직접 μ„€μ •ν•©λ‹ˆλ‹€. λ³€κ²½ 사항을 κΈ°λ‘ν•˜κ³  싢지 μ•Šμ•˜μ§€λ§Œ μ—…λ°μ΄νŠΈ ν•  각 λ·°κ°€ ν•„μš”ν–ˆκΈ° λ•Œλ¬Έμ— silent 도 μ‚¬μš©ν•  수 μ—†μ—ˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜μ„œ μ €λŠ” μ›μˆ­μ΄ 패치λ₯Ό μΌμŠ΅λ‹ˆλ‹€.

Backbone.Model.prototype.reset = (attributes, options) ->
    attrs = attributes || {};
    if options.parse
      attrs = this.parse(attrs, options) || {}

    @set(attrs, options);
    <strong i="9">@changed</strong> = {};

Backbone.Model Backbone.Collection 와 같은 μž¬μ„€μ • λ°©λ²•μ΄μ—†λŠ” μ΄μœ κ°€ κΆκΈˆν•˜μ‹­λ‹ˆκΉŒ?

question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

@thesmart , Model # set은 λˆ„λ½ 된 속성을 μ„€μ • ν•΄μ œν•˜μ§€ μ•ŠμœΌλ―€λ‘œ Model # reset이 ν•„μš”ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

이슈λ₯Ό μ’…κ²° ν•  λ•Œ 이유λ₯Ό μ œμ‹œν•΄μ•Όν•©λ‹ˆλ‹€. @ akre54 κ°€ μ™œ http://backbonejs.org/#Getting -startedμ—μ„œ

μ² ν•™μ μœΌλ‘œ Backbone은 JavaScript둜 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ ꡬ좕 ν•  λ•Œ 일반적으둜 μœ μš©ν•œ μ΅œμ†Œν•œμ˜ 데이터 ꡬ쑰 (λͺ¨λΈ 및 μ»¬λ ‰μ…˜) 및 μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€ (λ·° 및 URL) κΈ°λ³Έ μš”μ†Œλ₯Ό λ°œκ²¬ν•˜λ €λŠ” μ‹œλ„μž…λ‹ˆλ‹€.

λͺ¨λ“  22 λŒ“κΈ€

μž¬μ„€μ •μ€ νƒˆμΆœκ΅¬λ‘œ, ν•„μš”ν•œ 경우 λŒ€λŸ‰μœΌλ‘œ 효율적인 λ Œλ”λ§μ„ μ‰½κ²Œ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ·€ν•˜μ˜ κ²½μš°μ—λŠ” set 만 μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

속성 변경을 λ”λŸ½ νžˆμ§€ μ•Šκ³  μ„€μ •ν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

2014 λ…„ 8 μ›” 1 일 μ˜€μ „ 7:28에 Jeremy Ashkenas [email protected] 은 λ‹€μŒκ³Ό 같이 μΌμŠ΅λ‹ˆλ‹€.

μž¬μ„€μ •μ€ νƒˆμΆœκ΅¬λ‘œ, ν•„μš”ν•œ 경우 λŒ€λŸ‰μœΌλ‘œ 효율적인 λ Œλ”λ§μ„ μ‰½κ²Œ μˆ˜ν–‰ ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

κ·€ν•˜μ˜ κ²½μš°μ—λŠ” μ„ΈνŠΈλ₯Ό μ‚¬μš©ν•˜μ‹­μ‹œμ˜€.

β€”
이 이메일에 직접 λ‹΅μž₯ν•˜κ±°λ‚˜ GitHubμ—μ„œ ν™•μΈν•˜μ„Έμš”.

호좜 Model.clear() 전에 Model.set({}) κ·€ν•˜μ˜ κ²½μš°μ— μž‘λ™ν•©λ‹ˆλ‹€? μ΄λ ‡κ²Œν•˜λ©΄ ν˜„μž¬ 속성을 ν™•μž₯ν•˜μ§€ μ•Šκ³  λŒ€μ‹  λŒ€μ²΄ν•©λ‹ˆλ‹€. Model.clear() λŠ” λͺ¨λΈμ—μ„œ 두 개의 "λ³€κ²½"이벀트λ₯Ό νŠΈλ¦¬κ±°ν•˜μ§€ μ•ŠμœΌλ €λŠ” 경우 μžλ™ μ˜΅μ…˜λ„ μ§€μ›ν•©λ‹ˆλ‹€.

이 방법이 μ—†λ‹€λŠ” 데 λ™μ˜ν•©λ‹ˆλ‹€. model.clear() 와 model.set() λ₯Ό ν•¨κ»˜ μ‚¬μš©ν•˜λŠ” 것도 κ³ λ €ν–ˆμŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ λ¬Έμ œκ°€ λ°œμƒν•˜μ—¬ change 이벀트λ₯Ό 두 번 μ‹€ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.
model.clear() 호좜 ν•  λ•Œ silent μ˜΅μ…˜μ„ μ‚¬μš©ν•˜λŠ” 것은 μ˜΅μ…˜μ΄ μ•„λ‹™λ‹ˆλ‹€. 속성이 μ„€μ •λ˜μ§€ μ•Šμ„ λ•Œ change μ΄λ²€νŠΈκ°€ μ‹œμž‘λ˜κΈ°λ₯Ό μ›ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.

model.reset() λ©”μ†Œλ“œλŠ” μƒˆ 속성 ν•΄μ‹œλ₯Ό μ·¨ν•˜κ³  μƒˆ 속성 ν•΄μ‹œμ— μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” 이전 속성 킀에 λŒ€ν•œ undefined κ°’μœΌλ‘œμ΄ ν•΄μ‹œλ₯Ό 채 μ›λ‹ˆλ‹€.

Model.prototype.reset = function(attrs, options) {
    for (var key in this.attributes) {
        if (key in attrs) continue;
        attrs[key] = void 0;
    }

    return this.set(attrs, options);
};

λΏ‘λΏ‘

λŠ” μ–΄λ•Œ:

Model.prototype.reset = function(attrs, options) {
    for (var key in this.attributes) {
        this.unset(key, {silent:true});
    }
    return this.set(attrs, options);
};

μ•„λ‹ˆμš”, λ„μ›€μ΄λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. model.reset() μ „λ‹¬ν•˜λŠ” μƒˆλ‘œμš΄ attrs ν•΄μ‹œμ—λŠ”μ—†λŠ” λͺ¨λΈμ˜ ν˜„μž¬ 속성 ν•΄μ‹œμ— foo ν‚€κ°€ μžˆλ‹€κ³  κ°€μ • ν•΄λ³΄μ‹­μ‹œμ˜€. change:foo 이벀트λ₯Όλ“€μ„ λ•Œ μžλ™ λͺ¨λ“œμ—μ„œ model.unset() λ₯Ό μ‚¬μš©ν–ˆκΈ° λ•Œλ¬Έμ— μƒˆ κ°’ undefined 으둜 νŠΈλ¦¬κ±°λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

Backbone.Model.prototype.reset = function(attrs, options) {
    for (var key in this.attributes) {
        this.unset(key, {silent:true});
    }
    return this.set(attrs, options);
};

var bar = new Backbone.Model();

bar.on('change', function(model) {
    console.log('The model bar has been changed.');
});

bar.on('change:foo', function(model, foo) {
    console.log('Foo has been changed to: ' + foo);
});

bar.set(foo, 'test');
// => The model bar has been changed.
// => Foo has been changed to: test

bar.reset({ foo2: 'test2' });
// => The model bar has been changed.
// Foo was resetted but no change event has been triggered.

http://jsfiddle.net/lennerd/3s1Ltwgu/

멋지닀, 무슨 말인지 μ•Œ κ² μ–΄. this.attributesλ₯Ό λͺ…μ‹œ 적으둜 μž¬μ •μ˜ν•˜λŠ” this.unset(key, options) λ₯Ό μ‚¬μš©ν•˜λŠ” 것을 선택할 수 μžˆμ§€λ§Œ μ΄λŠ” attrs[key] = void 0; : panda_faceλ₯Ό κ΅μ²΄ν•˜λŠ” 문제 μΌλΏμž…λ‹ˆλ‹€.

λ‚΄κ°€ 아직 backbone.jsλ₯Ό 처음 μ ‘ν–ˆκΈ° λ•Œλ¬Έμ— λ‚΄κ°€ λ³΄μ—¬μ£ΌλŠ” λ¬΄μ§€λŠ” μš©μ„œν•˜μ§€λ§Œ λ…Όμ˜λ˜λŠ” λ™μž‘μ€ Model.fetch 처럼 λ“€λ¦½λ‹ˆλ‹€. μ„€λͺ… 은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

μ„œλ²„μ—μ„œ λͺ¨λΈμ˜ μƒνƒœλ₯Ό μž¬μ„€μ •ν•©λ‹ˆλ‹€.

change μ΄λ²€νŠΈκ°€ μ—¬μ „νžˆ 트리거 된 κ²ƒμ²˜λŸΌ λ³΄μ΄μ§€λ§Œ λͺ¨λΈμ„ "λ”λŸ½κ²Œ"λ§Œλ“€μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€. μ„œλ²„ 응닡을 기반으둜 속성을 μž¬μ„€μ •ν•˜λ €λŠ” 경우이 방법을 μ‚¬μš©ν•  μˆ˜μ—†λŠ” μ΄μœ κ°€ μžˆμŠ΅λ‹ˆκΉŒ? μ΄λŸ¬ν•œ 상황이 λ°œμƒν•  μˆ˜μžˆλŠ” μœ μΌν•œ 상황은 λͺ¨λΈμ΄ λ‹€λ₯Έ μž‘μ—…μ˜ λΆ€μž‘μš©μœΌλ‘œ λ³€κ²½λ˜λŠ” 경우라고 μƒκ°ν•˜μ§€λ§Œ 일반적으둜 μ΄λŸ¬ν•œ λΆ€μž‘μš©μ€ 잘λͺ»λœ ν”„λ‘œκ·Έλž˜λ°μœΌλ‘œ κ°„μ£Όλ˜λ―€λ‘œ κ°€λŠ₯ν•˜λ©΄ ν”Όν•΄μ•Όν•©λ‹ˆλ‹€. λΆ€μž‘μš©μ„ ν”Όν•  μˆ˜μ—†λŠ” 경우 μƒˆ λͺ¨λΈ 속성 λŒ€μ‹  "μ—…λ°μ΄νŠΈ λͺ¨λΈ XYZ"응닡 ν”Œλž˜κ·Έλ₯Ό μ „μ†‘ν•˜κ³  μ΄λŸ¬ν•œ 응닡을 λ³Ό λ•Œλ§ˆλ‹€ fetch νŠΈλ¦¬κ±°ν•˜λŠ” 것이 더 합리적 일 수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€μ‹œ λ§ν•˜μ§€λ§Œ,이 μ˜κ²¬μ—μ„œ λ‚΄κ°€ λ³΄μ—¬μ£Όκ³ μžˆλŠ” λͺ¨λ“  무지λ₯Ό μš©μ„œν•˜μ‹­μ‹œμ˜€.

@kolorahl ,

OPκ°€ λ‹¬μ„±ν•˜κ³ μžν•˜λŠ” 것은 ν˜„μž¬ λͺ¨λΈ 속성을 μ§€μš°κ³  λͺ¨λΈμ˜ μƒˆ μ†μ„±μ΄λ˜λŠ” μƒˆ JSON을 μ „λ‹¬ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ 이미 JSONμ΄μžˆλŠ” 경우 λ°±μ—”λ“œλ₯Ό κ³΅κ²©ν•˜κ³  싢지 μ•ŠμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 그의 clear + set λ°©λ²•μœΌλ‘œ @ lupugabriel1에 μ–΄λ–»κ²Œ λ“  λ™μ˜ν•©λ‹ˆλ‹€. ν•˜μ§€λ§Œ 이것이 κ³ λ €ν•΄μ•Ό ν•  κΈ°λŠ₯ 쀑 ν•˜λ‚˜λΌκ³  μƒκ°ν•©λ‹ˆλ‹€. Backbone.Collection # resetκ³Ό 같은 것

이것이 ν•„μš”ν•œ μ΄μœ λŠ” 세상이 λ³€ν•˜κ³  있기 λ•Œλ¬Έμž…λ‹ˆλ‹€. Backbone은 Model # fetch () XHR이 μ„œλ²„μ—μ„œ 데이터λ₯Όλ‘œλ“œν•˜λŠ” κΈ°λ³Έ 방법이라고 κ°€μ •ν•˜μ§€λ§Œ μ›Ή μ†ŒμΌ“μ„ μ‚¬μš©ν•˜μ—¬ 더 λ§Žμ€ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 데이터가 ν΄λΌμ΄μ–ΈνŠΈλ‘œ ν‘Έμ‹œλ˜λ©΄ .fetchλ₯Ό ν˜ΈμΆœν•˜λŠ” 것이 μ€‘λ³΅λ˜λ©° 데이터λ₯Ό μ‚¬μ΄λ“œλ‘œλ“œν•˜κ³  트리거 ν•  이벀트 후크λ₯Ό 얻을 μˆ˜μžˆλŠ” μ μ ˆν•œ 방법이 ν•„μš”ν•©λ‹ˆλ‹€.

#set λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

#set은 속성을 λ”λŸ½κ²Œ λ§Œλ“€ 것이기 β€‹β€‹λ•Œλ¬Έμ— @jridgewell . set을 μ‚¬μš©ν•˜μ—¬ μ–΄λ–€ 일이 λ°œμƒν•˜λŠ”μ§€ μ‚΄νŽ΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

function callback(data_from_server) {
  console.info(data_from_server);
  m = new Backbone.Model(data_from_server);
  m.set('foo', 'what?', {silent: true});
  console.info(m.changedAttributes())
}

μ‹€μ œ κ²°κ³Ό :

{foo: 'bar'}
{foo: "what?"}

μš”λ§λ˜λŠ” κ²°κ³Ό:

{foo: 'bar'}
false

μƒνƒœκ°€ μ„œλ²„μ™€ λ™κΈ°ν™”λ˜μ§€ μ•Šμ€ μƒνƒœλ‘œ λ³€κ²½λœ κ²½μš°μ—λŠ” 섀정해도 μ’‹μ§€λ§Œ μƒνƒœλ₯Ό μ„œλ²„μ™€ λ™κΈ°ν™”λ‘œ ν‘œμ‹œ ν•  방법이 μ—†κΈ° λ•Œλ¬Έμ— Model # reset이 ν•„μš”ν•©λ‹ˆλ‹€.

이 κΈ°λŠ₯에 λŒ€ν•΄ λ‹€λ₯Έ μ›μˆ­μ΄ 패치λ₯Ό μž‘μ„±ν–ˆμŠ΅λ‹ˆλ‹€.

/**
 * Allow for side-loading data from the server, calling the sync event afterwards.
 * <strong i="6">@param</strong> attributes
 * <strong i="7">@param</strong> options
 */
Backbone.Model.prototype.sync_set = function(attributes, options) {
  var attrs = attributes || {};
  if (options.parse) {
    attrs = this.parse(attrs, options) || {}
  }

  this.set(attrs, options);
  this.changed = {};
  this.trigger('sync', this, attributes, options);
  return this;
}
function callback(data_from_server) {
  console.info(data_from_server);
  m = new Backbone.Model(data_from_server);
  m.set('foo', 'what?', {silent: true});
  console.info(m.changedAttributes())
}
{foo: 'bar'}
false

λˆ„λ½ 된 ν•­λͺ©λ„ μ„€μ • ν•΄μ œν•΄μ•Όν•©λ‹ˆλ‹€. Model # set (attributes)이 μž‘μ—…ν•˜λŠ”μ§€ ν™•μ‹€ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

@lennerdκ°€ μ§€μ ν–ˆλ“―μ΄ clear() λ‹€μŒμ— set() ν˜ΈμΆœν•˜λŠ” 것은 쒋은 μ˜΅μ…˜μ΄ μ•„λ‹™λ‹ˆλ‹€.

1) 두 개의 change 이벀트λ₯Ό μ‹€ν–‰ν•˜κ³ 
2) clear ν˜ΈμΆœμ—μ„œ silent:true λ₯Ό μ‚¬μš©ν•˜λ©΄ μ„€μ •λ˜μ§€ μ•Šμ€ 속성에 λŒ€ν•œ λ³€κ²½ μ΄λ²€νŠΈκ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

collection.reset() λŠ” 맀우 직관적이며 Model λŠ” λ™μΌν•œ 방법을 μ‚¬μš©ν•˜λ©΄ μ‹€μ œλ‘œ 이점을 얻을 수 μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. λ‚˜λŠ” 항상 model.reset(attrs) λ₯Ό μ‚¬μš©ν•˜λ €κ³ ν•˜λŠ”λ°, 그것이 μ—†μœΌλ©΄ 항상 μ‹€λ§ν•©λ‹ˆλ‹€. :(

μ €λŠ” Backbone.Model : Backbone-Model-Reset에 μž‘λ™ν•˜λŠ” reset λ©”μ„œλ“œλ₯Ό μΆ”κ°€ν•˜κΈ° μœ„ν•΄ μ•½κ°„μ˜ ν™•μž₯을 λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.

λ™μ˜-기본적으둜 κ°–λŠ” 것이 μœ μš©ν•©λ‹ˆλ‹€. clear ()처럼 'id'속성을 λ³€κ²½ν•˜μ§€ μ•Šκ³  속성을 μž¬μ„€μ •ν•΄μ•Όν–ˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ° 에 그것 의

@thesmart , Model # set은 λˆ„λ½ 된 속성을 μ„€μ • ν•΄μ œν•˜μ§€ μ•ŠμœΌλ―€λ‘œ Model # reset이 ν•„μš”ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

이슈λ₯Ό μ’…κ²° ν•  λ•Œ 이유λ₯Ό μ œμ‹œν•΄μ•Όν•©λ‹ˆλ‹€. @ akre54 κ°€ μ™œ http://backbonejs.org/#Getting -startedμ—μ„œ

μ² ν•™μ μœΌλ‘œ Backbone은 JavaScript둜 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ ꡬ좕 ν•  λ•Œ 일반적으둜 μœ μš©ν•œ μ΅œμ†Œν•œμ˜ 데이터 ꡬ쑰 (λͺ¨λΈ 및 μ»¬λ ‰μ…˜) 및 μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€ (λ·° 및 URL) κΈ°λ³Έ μš”μ†Œλ₯Ό λ°œκ²¬ν•˜λ €λŠ” μ‹œλ„μž…λ‹ˆλ‹€.

λ‚΄κ°€ μ•Œ μˆ˜μžˆλŠ” ν•œ, Backbone의 데이터 λͺ¨λΈμ€ REST와 ν˜Έν™˜λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
ν΄λΌμ΄μ–ΈνŠΈμ—μ„œ μ„œλ²„ μƒνƒœλ₯Ό μ•ˆμ „ν•˜κ²Œ λͺ¨λΈλ§ ν•  μˆ˜μžˆλŠ” 방법이 μ—†κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
λͺ¨λΈμ΄ κ΅¬μΆ•λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μƒˆλ‘œμš΄ λͺ¨λΈμ„ κ΅¬μΆ•ν•˜λŠ” 것이
μž¬μ„€μ •ν•˜μ§€ μ•Šκ³  μƒˆλ‘œμš΄ μƒνƒœ.

2016 λ…„ 7 μ›” 5 일 ν™”μš”μΌμ— pgifford [email protected] 은 λ‹€μŒκ³Ό 같이 μΌμŠ΅λ‹ˆλ‹€.

@thesmart https://github.com/thesmart , Model # setκ°€ λˆ„λ½λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.
κ·Έλž˜μ„œ Model # reset이 ν•„μš”ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

이슈λ₯Ό μ’…κ²° ν•  λ•Œ 이유λ₯Ό μ œμ‹œν•΄μ•Όν•©λ‹ˆλ‹€. 신뢄증
@ akre54 https://github.com/akre54 κ°€ μ™œ 이것을 λ‹«μ•˜λŠ”μ§€ μ•Œκ³  μ‹ΆμŠ΅λ‹ˆλ‹€.
λ°±λ³Έ λͺ¨λΈμ€ μ˜λ„μ μœΌλ‘œ μ›μ‹œμ μ΄λ©° 의견이 μ—†μŠ΅λ‹ˆλ‹€.
Model # reset의 뢀쑱은 λͺ¨λΈμ΄ μ–΄λ–»κ²Œ μ‚¬μš©λ˜λŠ”μ§€μ— λŒ€ν•œ μ˜κ²¬μ„ ν‘œν˜„ν•©λ‹ˆλ‹€. μ—μ„œ
http://backbonejs.org/#Getting -started

μ² ν•™μ μœΌλ‘œ 백본은 μ΅œμ†Œν•œμ˜ μ„ΈνŠΈλ₯Ό λ°œκ²¬ν•˜λ €λŠ” μ‹œλ„μž…λ‹ˆλ‹€.
데이터 ꡬ쑰화 (λͺ¨λΈ 및 μ»¬λ ‰μ…˜) 및 μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€ (보기 및
URL) μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ λΉŒλ“œ ν•  λ•Œ 일반적으둜 μœ μš©ν•œ κΈ°λ³Έ μš”μ†Œ
JavaScript둜.

β€”
당신이 μ–ΈκΈ‰ λ˜μ—ˆκΈ° λ•Œλ¬Έμ— 이것을 λ°›κ³  μžˆμŠ΅λ‹ˆλ‹€.
이 이메일에 직접 λ‹΅μž₯ν•˜κ³  GitHubμ—μ„œ ν™•μΈν•˜μ„Έμš”.
https://github.com/jashkenas/backbone/issues/3253#issuecomment -230586424,
λ˜λŠ” μŠ€λ ˆλ“œ μŒμ†Œκ±°
https://github.com/notifications/unsubscribe/AARJLQ8-BGeV0X_owHVpyPQAdeiMweNMks5qSriDgaJpZM4CTBHH
.

λ‚˜λŠ” reset λͺ¨λΈμ΄ ν•„μš”ν–ˆκ³  Backbone의 _spirit_μ—μ„œ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” 이것을 Backbone의 개인적인 ν™•μž₯μ—μ„œ μ‚¬μš©ν•©λ‹ˆλ‹€.

기본값을 λ‹€μ‹œ λͺ¨λΈμ— λ³‘ν•©ν•˜μ—¬ μ΄ˆκΈ°ν™” ν•  λ•Œμ²˜λŸΌ 전달 된 속성을 μž¬μ •μ˜ ν•  수 있기 λ•Œλ¬Έμ— λ‹¨μˆœν•œ .clear λ‹€μŒμ— .set .clear μ˜€λŠ” 것보닀 λ‚«μŠ΅λ‹ˆλ‹€.

/**
 * Clears the model's attributes and sets the default attributes.
 * <strong i="10">@param</strong> {Object} attributes to overwrite defaults
 * <strong i="11">@param</strong> {Object} options  to pass with the "set" call.
 * <strong i="12">@return</strong> {Backbone.Model}  this object, to chain function calls.
 */
reset: function(attributes, options) {
    options = _.extend({ reset: true }, options);

    // ensure default params
    var defaults = _.result(this, 'defaults'),
        attrs = _.defaults(_.extend({}, defaults, attributes || {}), defaults);

    // apply
    this._reset(attrs, options);

    // triggers a custom event, namespaced to model in order
    // to avoid collision with collection's native reset event
    // when listening to a collection.
    if (!options.silent) this.trigger('model:reset', this, options);

    return this;
},

/**
 * Private method to help wrap reset with a custom behavior in child
 * classes.
 * <strong i="13">@param</strong>  {Object} attributes to overwrite defaults
 * <strong i="14">@param</strong>  {Object} options  to pass with the "set" call.
 */
_reset: function(attrs, options) {
    this.clear({ silent: true }).set(attrs, options);
},

silent: true κ°€ μ•„λ‹Œ 경우 μ‚¬μš©μž 지정 model:reset 이벀트 외에도 λ³€κ²½ 이벀트 ( change 및 change:attribute )λ₯Ό νŠΈλ¦¬κ±°ν•©λ‹ˆλ‹€. model:reset 이벀트 만 νŠΈλ¦¬κ±°ν•˜λ„λ‘ μ‰½κ²Œ μ‚¬μš©μž μ •μ˜ ν•  수 μžˆμ§€λ§Œ λͺ¨λΈ μž¬μ„€μ •μ‹œ 항상 λ³€κ²½ μ΄λ²€νŠΈκ°€ νŠΈλ¦¬κ±°λ˜μ–΄μ•Όν•œλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

λ‚˜λŠ” lennerd https://github.com/jashkenas/backbone/issues/3253#issuecomment -58789524의 μ†”λ£¨μ…˜μ„ μ•½κ°„ μ‘°μ •ν–ˆμŠ΅λ‹ˆλ‹€.

Backbone.Model.prototype.reset = function(attrs, options) {
    var missing = {};
    for (var key in this.attributes) {
        if (key in attrs) continue;
        attrs[key] = undefined;
        missing[key] = true;
    }
    // trigger all change events at the same time
    this.set(attrs, options);
    // remove missing attributes completely
    for (var key in missing) {
        // no silent option here in case  attributes changed again meanwhile (edge case)
        this.unset(key)
    }
    return this;
};
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰