cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
972
Views
0
Helpful
2
Replies

loading new Tolopogy

m.rainer
Level 1
Level 1

Hi,

I am playing around for hours now. But whatever I try it does not work.


I would like to load new Topologies from backend system. (REST Interface)

I started with the example from showcase: "Topology with remote Data".

But the propertie defined there is never touched by data...

So I changed my code to a getter and setter methode.

methods: {

            getTopo: function() {

            var result = this.view('topo').getData();

                return result;

            },

            setTopo: function (newTopo) {

                 this.view('topo').data(newTopo);

                 var c = this.getTopo();

            }

        }

When calling this function on app start everything works fine:

(function (nx) {

    var App = nx.define(nx.ui.Application, {

        methods: {

            start: function() {

                node = new Tooltip.Node();     

                node.setTopo(initDataAlt);

                var model = new com.cisco.TopologyModel();

                node.attach(this);

                node.model(model);

            }

        }

    });

    new App().start();

})(nx);

But when calling the setter from JavaScript Button Action to load a new Topology nothing happens.

In Variable "newTopo" there is the new topologie loaded BUT in variable "c" there is still the old topology.

If changing the call to: "this.view('topo').setData(newTopo)" does not change anything.

Do I have to clear topo before setting again? If so how can I do that?

Any idea?

Thanks a lot &

Best regards

Markus

An

;

1 Accepted Solution

Accepted Solutions

alzverev
Cisco Employee
Cisco Employee

Hi Markus.

It's a better idea to use 'getTopo' to get the topology instance instead of getting topology data. In this case you're gonna have something like that:

methods: {

     'getTopo': function(){

          return this.view('topo');

     }

You'll be able to use any methods of nx.graphic.Topology class just having invoked .getTopo():

1. to update topology data: topologyContainer.getTopo().data(newTopo);

2. to read exisiting data: topologyContainer.getTopo().data();

Of course, you can also assign topologyContainer.getTopo() reference to a variable, like that...

var topo = topologyContainer.getTopo();

... and then use it, like that:

var topo = topologyContainer.getTopo();

$.ajax({

     type: 'GET',

     url: 'http://example.com/topology',

     dataType: 'json',

     // ... some other config ...

     success: function(data){

          // use try/catch to track parsing errors

          try {

               var data = JSON.parse(data);

               topo.data(data);

          }

          catch(SyntaxError){

               alert('hey, json is broken');

          }

     },

     error: function(jqXHR, exception){

          // some http erorr stuff

          alert("couldn't get topology from the server");

     }

});

View solution in original post

2 Replies 2

alzverev
Cisco Employee
Cisco Employee

Hi Markus.

It's a better idea to use 'getTopo' to get the topology instance instead of getting topology data. In this case you're gonna have something like that:

methods: {

     'getTopo': function(){

          return this.view('topo');

     }

You'll be able to use any methods of nx.graphic.Topology class just having invoked .getTopo():

1. to update topology data: topologyContainer.getTopo().data(newTopo);

2. to read exisiting data: topologyContainer.getTopo().data();

Of course, you can also assign topologyContainer.getTopo() reference to a variable, like that...

var topo = topologyContainer.getTopo();

... and then use it, like that:

var topo = topologyContainer.getTopo();

$.ajax({

     type: 'GET',

     url: 'http://example.com/topology',

     dataType: 'json',

     // ... some other config ...

     success: function(data){

          // use try/catch to track parsing errors

          try {

               var data = JSON.parse(data);

               topo.data(data);

          }

          catch(SyntaxError){

               alert('hey, json is broken');

          }

     },

     error: function(jqXHR, exception){

          // some http erorr stuff

          alert("couldn't get topology from the server");

     }

});

Hello Aleksei,

Thanks a lot for your answer.

This is exactly what I was missing:

var data = JSON.parse(data);

I tried to set a String instead of JSON Object.
Thanks a lot

Best regards

Markus