Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add once to Utils #186

Merged
merged 1 commit into from
Oct 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions doc/utils_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ function on the leading instead of the trailing edge of the `wait` interval.
<a name="utils.delegate"></a>
## utils.delegate(rules)

Delegate to event handlers based on the event target. The `rules` argument is an object who's keys represent keys
Delegate to event handlers based on the event target. The `rules` argument is an object who's keys represent keys
in the component's `attr` object (resolving to the selector of the event target) and values are the handler to be
called.
called.

The handlers are lazily resolved when the event is fired.

Expand Down Expand Up @@ -113,3 +113,22 @@ Convert an object to an array. Optionally specify the index at which to begin ex

Can produce only unique arrays of homogeneous primitives, e.g., an array of
only strings, an array of only booleans, or an array of only numerics

<a name="utils.once"></a>
## utils.once(func)

Ensures that a function will only be called once.
```js
var sum = 0;
var increment = utils.once(function () { sum++; });
increment(); // sum will equal 1
increment(); // sum will still equal 1
```

Will only send one DELETE request to the server even if the click event is fired multiple times.
```js
var myHanlder = function () {
$.ajax({type: 'DELETE', url: 'someurl.com', data: {id: 1}});
};
this.on('click', utils.once(myHandler));
```
27 changes: 27 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,33 @@ define(
}
}, this);
};
},

// ensures that a function will only be called once.
// usage:
// will only create the application once
// var initialize = utils.once(createApplication)
// initialize();
// initialize();
//
// will only delete a record once
// var myHanlder = function () {
// $.ajax({type: 'DELETE', url: 'someurl.com', data: {id: 1}});
// };
// this.on('click', utils.once(myHandler));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would add this example to the docs too

//
once: function(func) {
var ran, result;

return function() {
if (ran) {
return result;
}
ran = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this appear after the function has been called?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will change.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still needs to be fixed

result = func.apply(this, arguments);

return result;
};
}

};
Expand Down
10 changes: 10 additions & 0 deletions test/spec/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,14 @@ define(['lib/component', 'lib/utils'], function (defineComponent, utils) {
});
});

describe('once()', function () {
it('should only call a function once', function () {
var sum = 0;
var increment = utils.once(function () { sum++; });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/utils/util

increment();
increment();
expect(sum).toEqual(1);
});
});

});