1

I am working on a MVC application and I need to read resource file and get values , pass them in a javascript file. For this I am just calling a js function written in separate js file, from cshtml file. This function passes parameters which have the resource file value, to the function in javascript. Now in js file, I have another function in the same namespace as my above function where I need to read these resource file values. How can I do that?

My cshtml code:

<script type="text/javascript">
Mynamespace.function2("@Globalstrings.Value1","@Globalstrings.Value2");
</script>

JS file:

var Mynamespace ={
    function1: function(){
        Mynamespace.function2.getMess();   // I need to access value1 here in this function
    },

    function2: function(message1,message2){
        var value1 = message1;
        var value2 = message2;
        return{
          getMess: function(){ return value1;},
          getLab: function() { return value2;}
        }
    }
}

When I tried the above, I am getting undefined in getMess. So, I could have 2 options primarily which can be accepted as an answer for this:

  1. Any other way of reading resource file value in js?
  2. Any fix for the above js code

I actually posted this question few hours ago (Accessing variable in another function, returns undefined - JavaScript), but made it limited to javascript scoping problem only,but then realised I will post the actual problem.

Any help would be appreciated.

1 Answer 1

2

You need a comma in your return object for setvalues. It's still not going to work, but at least that fixes your syntax error.

return {
    getMess: function(){ return value1;},
    getLab: function() { return value2;}
}

Also, it seems kind of odd returning the value1 and value2 in setvalue(), could I suggest returning the values in the getter? Fixed and refactored in this Fiddle

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

5 Comments

Thanks for pointing these things. Sorry for that, was a mistake. Edited and also changed the function names to avoid confusion.
Were you able to look at my fiddle? I offered a couple of suggestions.
Thanks a lot man, it worked. :-) I have few question though, just to understand the solution better and enhance my knowledge: 1) The solution you provided takes 2 different functions, one for storing values and other for fetching it, wouldn't it be possible to somehow manage in one. 2) This kind of solution (get, set) is generally followed in server side language, correct me if wrong. Would there be any similar pattern in js to write it.
Also, would it mean that the value1, value2 would exist in global namespace, kind of them being global variable.
Awesome! Glad I could help. Value1 and Value2 are stored in the Mynamespace Object. As far as the (get,set) pattern is concerned. Some libraries in Javascript use the arguments local variable in the function to decide what to do based on how many arguments you passed in. For example, in jQuery $('p').text() is a getter and $('p').text('hello!') is a setter. This blog post explains the arguments variable in detail justinbar.net/post/… , so you could do something slick with that.

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.