Git Product home page Git Product logo

Comments (5)

colinta avatar colinta commented on July 18, 2024

for (attr in _(attrs).keys()) {} is wrong! I can't figure out a clever shorthand, but this works:

for (var attr in attrs) {
  if ( ! attrs.hasOwnProperty(attr) )
    continue;
  // ...
}

from backbone.getters.setters.

berzniz avatar berzniz commented on July 18, 2024

Thanks for all the input.

1. The prototyping bug. can you include a scenario where the problem can be seen? In most cases, the backbone API for the "key" argument is a javascript object only (JSON) or a single key. Note that the original backbone source code includes the same kind of enumeration over the "attrs" variable inside the original "set" method.

However, there are bugs in the code that I haven't pushed into the repo yet, mainly not declaring "attrs", "attr" with a "var" before them. Will update this soon.

2. The "formatter feature". I see the need for a more versatile getter method, but I think this can be done in a simpler way.

One option that comes to mind is getting the "datetime" Date object and manipulating it using external methods. I think the date methods should be implemented on the Date object itself (check out date.js for a better javascript Date object).

For other tasks requiring methods on the backbone object, Another option is having multiple getter methods, one for each need (might not be suitable for the date-format task, but pretty useful for others): "timedate" and "timedateHHMMSS".

3. The raw-get function is good, but this might lead developers into calling this directly on the object and therefore "jumping over" the getter method. The idea is to have all get calls redirected into one getter method (I understand anyone can just call Backbone.Model.prototype.set.call but making this more accessible is not always recommended).

What do you think? I'm trying to favor small and simple code-base over more features.

from backbone.getters.setters.

colinta avatar colinta commented on July 18, 2024

The prototyping bug is for people who add methods to Object.prototype. I don't do that, but there are still those masochists who use Prototype.

backbone and underscore sometimes (not always!) use for ( prop in attars ) {}, but they also use _.has (a synonym for Object.hasOwnProperty), and the times they use one or the other depend on whether they want all the attributes of the object or just the "keys" (case in point: they _.keys function uses _.has).

In this case, you only want the "setter-keys", not Object.prototype methods AND keys.

Next up. Actually, rawget was specifically to bypass the getters/setters! So yeah, if you don't want a way to do that then drop 'em, for sure.

The formatter feature seems useful to me, I'm not sure how it could get "simpler", I'm just passing the format along to the getter/setter. I won't try too hard to convince you. It's something I got used to in my company's ORM (we call 'em "formatters", and there's a whole system for them). I might even add this feature in my fork.

I think they makes things short and sweet, though it is a little magic...

Formatter.register('$', function(value) { return '$' + value; });
event.cost('$')  // => '$5.00'

Up to you! Except the Object.prototype thing... 😉

from backbone.getters.setters.

zelibobla avatar zelibobla commented on July 18, 2024

It looks like using model.toJSON() passing getters

from backbone.getters.setters.

berzniz avatar berzniz commented on July 18, 2024

As I said, I'm trying to favor small and simple code-base over more features.

from backbone.getters.setters.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.