1

I am trying to implement module pattern in my code according to some examples online, what I am trying to achieve is to simply bind a button click event in my html to a function (which is not working), below is my HTML:

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@*" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
    <script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body>
    <h1>Hello Plunker!</h1>
    <input type="button" id="btn-msg" value="click me"/>
  </body>

</html>

and here is my JS:

//CRUD Start
var Rutherford = Rutherford || {};
Rutherford.crud = function() {

  function _readLists() {
    alert("am here");
  }

  return {
    readLists: _readLists
  }
}

Rutherford.Initiate = function() {
  $("#btn-msg").click(Rutherford.crud.readLists);
}

$(function() {
  Rutherford.Initiate();
});

Here is as well a link to my plunker: http://plnkr.co/edit/tA94lzMPHkUOr8QuyJK8?p=preview

All what am trying to achieve is to bind the button to the function.

2 Answers 2

3

You need to call the anonymous function, not assign it. See the () below:

Rutherford.crud = (function() {

  function _readLists() {
    alert("am here");
  }

  return {
    readLists: _readLists
  }
}());

Here's an updated plunkr with this change: http://plnkr.co/edit/uiWHmtkMFEKywvFRk6DF?p=info

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Brandon, it's still not working for me, can you show it to me on plunker?
0

I believe that Evan Knowles wanted to say this:

//CRUD Start
var Rutherford = Rutherford || {};
Rutherford.crud = (function() {

  function _readLists() {
    alert("am here");
  }

  return {
    readLists: _readLists
  }
})( );

Rutherford.Initiate = function() {
  $("#btn-msg").click(Rutherford.crud.readLists);
}

$(function() {
  Rutherford.Initiate();
});

This would work properly if you can use Rutherford as a Singleton.

3 Comments

But doesn't Rutherford.crud = (function() { function _readLists() { alert("am here"); } return { readLists: _readLists } }()); execute the code right away? I want to store the function definition in crud, but the IIFY will execute it right away, right?
This depends on the way you want to use this crud function. With this closure the code will be executed at parsing time, not when you call Initiate. This will mean that crud object contain only the function's returned object, not the definition. If you want preserve the definition and want to reuse crud function, you have to execute it in Initiate, creating new object. I think the first solution is better for your cases.
I want to have the crud function as a utility to store all my crud operations, like insert/delete etc.. what's the best way for it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.