CanJS click event implementation

1k Views Asked by At

I am using CanJS (a js framework) for my project, but I have a problem.

I've written some code in book.js as follows:

Book = can.Model({
    create: 'POST site/book'
},{});

Books = can.Control({
        
        '.gotoPrevPage click' : function(){
            b= new Book({name:"newbook1"});
            b.save();
        }
});

and also, my html file is as follows:

...
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/can.jquery-1.0.7.min.js"></script>
<script type="text/javascript" src="js/book.js"></script>
...
<div class="twopageDoc">

    <div class="gotoPrevPage"></div>

    <div class="leftPage">

        <img src="images/1.jpg" />

    </div>

    <div class="rightPage">

        <img src="images/2.jpg" />

    </div>

    <div class="gotoNextPage"></div>

</div>
...

Now, when I click on div with class gotoPrevPage event doesn't work and I don't see any errors, but when I use Chrome console and write down the 2 lines code of the above event the POST is done and it works. I don't know the reason.

1

There are 1 best solutions below

2
On

From your code it looks like you didn't initialize the controller. You should create instance of the Books on the DOM element:

new Books('.twpopageDoc')