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.

4

1 回答 1

2

您需要在返回对象中使用逗号作为设置值。它仍然无法正常工作,但至少可以修复您的语法错误。

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

另外,返回 value1 和 value2 in 似乎有点奇怪setvalue(),我可以建议返回 getter 中的值吗?在这个Fiddle中修复和重构

于 2013-10-18T16:25:40.957 回答