0

I'm having a function. In which data get value from getValue() function.

Below code is a fragment.

grid.js

function gridLoadComplete(){
    var data = getValue();      
}

Consider below HTML's

  • Index.html - Contains two below fragments
    • staffGrid.html
    • studentGrid.html

I have added grid.js fragment to staffGrid.html & teacherGrid.html.

When i load index.html it get's error. I don't get value in data because it's called twice.

Is there any way to resolve this problem. using the same function name getvalue() ??


10
  • So, you are trying to call grindLoadComplete() from index.html, but have it grid.js linked to staffGrid.html and teacherGrid.html, correct? Try linking grid.js to index.html. These are three separate files that won't share sources, unless you have it called by a fourth file where it combines the three (not with frames, that won't work). Commented Sep 26, 2014 at 12:21
  • I can't link grid.js to index.html. Bcoz getValue() function returns different values in staffgrid & studentgrid.html Commented Sep 26, 2014 at 12:26
  • 1
    How have you included them as fragments? Commented Sep 26, 2014 at 12:28
  • The fact that they return different values should have any impact of including it. What you have described is that you have three separate files, two of which are linked to the grid.js file while the third is where you need to call from. Now you say that it displays different information in the staffGrid.html than the studentGrid.html, indicating you need it called from them, not index.html. Perhaps you can explain how the three files are connected so we understand why you need to call the function from index.html instead of staffGrid.html and strudentGrid.html. Commented Sep 26, 2014 at 12:31
  • @coderboi_89 Tht's my part of mistake. I have corrected the question.. Commented Sep 26, 2014 at 12:39

2 Answers 2

2

Looks like your issue rises from the fact that you have too many globals and that your code is not modular.

One solution would be to write your grid.js file in a way that's exposing a Grid component which can be instanciated and which encapsulates it's logic, so that the index.html page can create independent Grid component instances.

You can think of your grid or any UI component as re-usable components such as native <select> element.

What could you do with 2 select elements in the same page?

index.html

$(function () {
    var $select1 = $('#select-1'), //this could be staffGrid = new Grid('#staff-grid')
        $select2 = $('#select-2');

    //Make a parallel with the change event and your gridLoadComplete event.
    //gridLoadComplete should be fired by your grid component itself and your component
    //should allow to listen to those events.

    //It could be staffGrid.on('loadComplete', function () { staffGrid.getValue(); });
    $select1.change(function () {
        $select1.val(); //access select1 value
    });

    $select2.change(function () {
        $select2.val(); //access select2 value
    });
});

As you can see in the exemple above, we invoke the same val function to get the value, but against different instances which allows us to get the value we want without duplicating the val function's logic.

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

1 Comment

Thanks man.! Modular approach can solve my issue.. Good Day..!
0

You should consider namespacing your javascript, such that each fragment has its own namespace. For example:

staffGrid.html would contain:

<script>
var staffGrid = {
    gridLoadComplete: function() {
        var data = getValue();
    },

    myOtherFunc: function() {
        //Do other stuff...
    }
};
</script>

Then in your index.html, you can call: staffGrid.gridLoadComplete(); and teacherGrid.gridLoadComplete();

1 Comment

That doesn't solve the getValue issue. It would if getValue was added to your object literal, but then you would have duplicated logic across your grid files.

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.