Calling a function on a controller from the view in v2.0 fails??

0 views
Skip to first unread message

Mark

unread,
Dec 21, 2009, 5:40:35 AM12/21/09
to JavaScriptMVC
Hi there,

I am nearly finished my migration from 1.5 to 2.0 but i have something
failing in my view... basically my view i have an <% and closed %>

I have a inside this this.formatNumber - but it gives an error.. its
not my function, i have put in a breakpoint in the function and it
never arrives

this is the signature

formatNumber: function(theNumber, theDecimals, theSeperator_decimal,
theSeperator_thousands) {

Has something changed in v2.0 to stop me from calling this function in
my controller from the view..??

here is an abtract of my view

<div id="ContentItems" style="float: left; padding: 2px; width:
100%;">
<%
var dailyPrice = this.formatNumber
(data.vehicles[i].dailyPrice, 2, '.', ',').toString().split('.');
var totalPrice = this.formatNumber
(data.vehicles[i].totalPrice, 2, '.', ',').toString().split('.');

I would appreciate any input on this... I think something has changed
but i have tried the docs and forum and don't see any solution.

If i can provide further info.... please let me know..

Thanks in advance..

Justin Meyer

unread,
Dec 21, 2009, 9:33:46 AM12/21/09
to javasc...@googlegroups.com
You have to pass the controller to the data in 2.0.   However, you should be making a view helper.

  
Justin Meyer

Jupiter Consulting
\Development\Training\Support
847-924-6039
justin...@gmail.com



--

You received this message because you are subscribed to the Google Groups "JavaScriptMVC" group.
To post to this group, send email to javasc...@googlegroups.com.
To unsubscribe from this group, send email to javascriptmv...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/javascriptmvc?hl=en.



Mark

unread,
Dec 21, 2009, 10:32:33 AM12/21/09
to JavaScriptMVC
Thanks justin, ok... no problems.. I want to do it correctly.

I think passing the controller is a bit heavy as i only need the one
function within my controller... so i presume this is why you say i
should be making a view helper..

Sorry to sound dumb, what is a view helper and how do i go about
creating one?

My funciton in my controller is used by my controller also but the
view needs it too..

Any pointers would be really helpful.. I have almost finished with the
migration from 1.5 to 2.0 i think

Thanks

On Dec 21, 3:33 pm, Justin Meyer <justinbme...@gmail.com> wrote:
> You have to pass the controller to the data in 2.0.   However, you should be
> making a view helper.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support
> 847-924-6039

> justinbme...@gmail.com

> > javascriptmv...@googlegroups.com<javascriptmvc%2Bunsu...@googlegroups.com>

Mark

unread,
Dec 21, 2009, 10:35:53 AM12/21/09
to JavaScriptMVC
I presume a view help will allow me to create "re-use" my function in
the controller?? or do i need to create a completely new function(view
helper) just for the view... This means i will have technically 2
functions that do the same.

I did check the docs.. and saw some stuff about view helper but it
didn't really help

Also would it be possible to pass in just the function to "data"
rather than the whole controller..

Any help you can offer here woudl be really great

thanks

On Dec 21, 3:33 pm, Justin Meyer <justinbme...@gmail.com> wrote:

> You have to pass the controller to the data in 2.0.   However, you should be
> making a view helper.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support
> 847-924-6039

> justinbme...@gmail.com

> > javascriptmv...@googlegroups.com<javascriptmvc%2Bunsu...@googlegroups.com>

Justin Meyer

unread,
Dec 21, 2009, 11:34:41 AM12/21/09
to javasc...@googlegroups.com
Passing the controller is no more heavyweight then passing the function.  Remember that almost everything in JS is a pointer/reference.  Passing a reference to a controller is no different than passing a reference to a function.

However, you can also pass the function, but 'this' will be be correct.


On view helpers, these will get populated from your controller.  So if you have TodoController.  You can add helpers like:

TodoController.Helpers = {
   foo : function(){}
}

Your controller actions could reference them like:

click : function(){
  this.Class.Helpers.foo()
}

This is slightly better then passing the funciton or controller b/c it keeps straight view formatting code out of the controller (which should really be responding to events and manipulating the DOM w/ the help of views.



Justin Meyer

Jupiter Consulting
\Development\Training\Support
847-924-6039
To unsubscribe from this group, send email to javascriptmv...@googlegroups.com.

Mark

unread,
Dec 21, 2009, 3:50:11 PM12/21/09
to JavaScriptMVC
Thank you justin, this is great! ....

I am always a person that tries to follow the rules put in place....
so with your explanation i will now start to create some view
helpers..

Thanks once again.

On Dec 21, 5:34 pm, Justin Meyer <justinbme...@gmail.com> wrote:
> Passing the controller is no more heavyweight then passing the function.
>  Remember that almost everything in JS is a pointer/reference.  Passing a
> reference to a controller is no different than passing a reference to a
> function.
>
> However, you can also pass the function, but 'this' will be be correct.
>
> On view helpers, these will get populated from your controller.  So if you
> have TodoController.  You can add helpers like:
>
> TodoController.Helpers = {
>    foo : function(){}
>
> }
>
> Your controller actions could reference them like:
>
> click : function(){
>   this.Class.Helpers.foo()
>
> }
>
> This is slightly better then passing the funciton or controller b/c it keeps
> straight view formatting code out of the controller (which should really be
> responding to events and manipulating the DOM w/ the help of views.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support847-924-6039

> justinbme...@gmail.com

> > <javascriptmvc%2Bunsu...@googlegroups.com<javascriptmvc%252Buns...@googlegroups.com>

Mark

unread,
Dec 21, 2009, 3:58:33 PM12/21/09
to JavaScriptMVC
One this, do i need to include any additional plugins or anything in
init.js?

and you state i can do this

TodoController.Helpers = {
foo : function(){}

}

I presume this goes in the static section of the controller???

Or do i create a brand new .js file? in the controllers sub directory?

here is my sample v2.0 controller

$.Controller.extend('ReservationContentController',
{
onDocument: true
// SHOULD i add the helper here in the form of
ReservationContentController.Helper?? i presume a coma after the
onDocument:true?
}
},
{
load: function(params) {
.........

On Dec 21, 5:34 pm, Justin Meyer <justinbme...@gmail.com> wrote:
> Passing the controller is no more heavyweight then passing the function.
>  Remember that almost everything in JS is a pointer/reference.  Passing a
> reference to a controller is no different than passing a reference to a
> function.
>
> However, you can also pass the function, but 'this' will be be correct.
>
> On view helpers, these will get populated from your controller.  So if you
> have TodoController.  You can add helpers like:
>
> TodoController.Helpers = {
>    foo : function(){}
>
> }
>
> Your controller actions could reference them like:
>
> click : function(){
>   this.Class.Helpers.foo()
>
> }
>
> This is slightly better then passing the funciton or controller b/c it keeps
> straight view formatting code out of the controller (which should really be
> responding to events and manipulating the DOM w/ the help of views.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support847-924-6039

> justinbme...@gmail.com

> > <javascriptmvc%2Bunsu...@googlegroups.com<javascriptmvc%252Buns...@googlegroups.com>

Mark

unread,
Dec 22, 2009, 5:48:30 AM12/22/09
to JavaScriptMVC
Thanks justin, i seem to have it working but i have no idea if i am
doing it correct-- I would appreciate some input.

I have tested calling my helper from my controller with
this.Class.Helpers and it works... but i can't seem to call my helper
from my view -- ??

Also this what i di.. i placed my
ReservationContentController.Helpers in the same file as my
controller but after my controller code, is this bad practice? ...
Where should it go? in its own file, which directory??

$.Controller.extend('ReservationContentController',
{
onDocument: true

},
{
load: function(params) {

//this.Class.Helpers.foo(); // COMMENTED OUT BUT IT DOES
WORK

}
});


ReservationContentController.Helpers = {
foo: function() { alert('hi'); }
}

On Dec 21, 5:34 pm, Justin Meyer <justinbme...@gmail.com> wrote:
> Passing the controller is no more heavyweight then passing the function.
>  Remember that almost everything in JS is a pointer/reference.  Passing a
> reference to a controller is no different than passing a reference to a
> function.
>
> However, you can also pass the function, but 'this' will be be correct.
>
> On view helpers, these will get populated from your controller.  So if you
> have TodoController.  You can add helpers like:
>
> TodoController.Helpers = {
>    foo : function(){}
>
> }
>
> Your controller actions could reference them like:
>
> click : function(){
>   this.Class.Helpers.foo()
>
> }
>
> This is slightly better then passing the funciton or controller b/c it keeps
> straight view formatting code out of the controller (which should really be
> responding to events and manipulating the DOM w/ the help of views.
>
> Justin Meyer
>
> Jupiter Consulting
> \Development\Training\Support
> 847-924-6039

> justinbme...@gmail.com

> > <javascriptmvc%2Bunsu...@googlegroups.com<javascriptmvc%252Buns...@googlegroups.com>

Justin Meyer

unread,
Dec 22, 2009, 10:05:25 AM12/22/09
to javasc...@googlegroups.com
walk in this.view()

you should see it add ReservationContentController.Helpers to the helpers.  If you don't let me know.


You should be able to do <%=foo()%>



Justin Meyer

Jupiter Consulting
\Development\Training\Support
847-924-6039
justin...@gmail.com


To unsubscribe from this group, send email to javascriptmv...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages