Comments (5)
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.
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.
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.
It looks like using model.toJSON() passing getters
from backbone.getters.setters.
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backbone.getters.setters.