cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

how to reduce javascript lines by jason file?

InfinityX
14-Alexandrite

how to reduce javascript lines by jason file?

i have created one experience in which there are 1000 lines of javascripts and i want to reduce to few lines ? how to achieve this by editing JSON file? can anyone help in this?

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @InfinityX ,

additional to the info provided by   @sdidier  I want to add that there is no exact answer of this question and it relevance of possible solution depends on the particular case.

So, in some case the usage of JSON construct could be very helpful when you for example assignee some property of object so you can do this in the Json file and then interpret this file in you js code. To read a json file you can use some construct like this:

 

//Global Variables
var pjson ="mfg.json";
var jsonData;

gotJSON=function(data){
 
  console.log("gotJSON:Working on DATA >>>>"+data);
  console.warn(data);
  try{jsonData=JSON.parse(data);} catch(ex){console.log('exception');console.warn(ex);}
  $scope.view.wdg.JSONList.list=jsonData.Steps;
  $scope.view.wdg.JSONList.label='Title';
  $scope.view.wdg.JSONList.value=jsonData.Steps[0].Title;
 }

doRead=function (jsonFile){
fetch(jsonFile)
  .then(response=>response.text())
  .then(data=>gotJSON(data))
}

$scope.Init=function() {
doRead('./app/resources/Uploaded/'+pjson);
}
/////
$scope.$on('$ionicView.afterEnter',function(){
$scope.Init();})

 

Depending on this what your json file implements then you can do different things -.  for e.g. setting multiple property:

 

$scope.app.JsonTest= function(){
//store in a global variabe some json properties for testing
//
var b={"chuck":{"visible":false,"shader":""},"chuckHolder":{"visible":false,"opacity":"1"},"chipHolder":{"visible":false,"opacity":"1"},
       "chipScrew":{"visible":false,"shader":""},"chipBolt":{"visible":false,"opacity":"1"},"estop":{"visible":false,"opacity":"1"},
       "frontDecal":{"visible":false,"shader":""},"spindleDecal":{"visible":false,"opacity":"1"},"estopFront":{"visible":false,"opacity":"1"}
      }  
$scope.JsonTestList=b;
$scope.setMutipleProps($scope.JsonTestList);  

}
//======= definition of set multiple props
$scope.setMutipleProps= function(obj){
Object.keys(obj).forEach(function (item){
            for(var prop in obj[item])
            						{                   
                   console.log("obj["+item+"]["+prop+"]="+obj[item][prop] )  
                  $scope.view.wdg[item][prop]=obj[item][prop]                   
                   console.log("==>$scope.view.wdg["+item+"]["+prop+"]="+obj[item][prop] )                  }
										})
                         
};

 

This and  a lot of other things  - but as already mentioned above this depend on your application case but also on your imagination, creativity and last but not least on your javascript skills.

 

 

View solution in original post

2 REPLIES 2
sdidier
17-Peridot
(To:InfinityX)

Hello InfinityX,

 

This question is highly depending of source code of Javascript.

 

By the way, JSON file is to store data and javascript is a script program.

 

At first, I will see if my code is redondant in Javascript.

By using a library file, it will be more maintenable.

At second, I will see if some CSS stylesheet and HTML strings are present inside it. 

It is better to have a CSS stylesheet and to modify Properties in Javascript.

The same with HTML strings.

 

It is needed to have more details about he JSON file and Javascript in your Project, to have a more accurate solution.

 

Best regards,

Samuel

Hi @InfinityX ,

additional to the info provided by   @sdidier  I want to add that there is no exact answer of this question and it relevance of possible solution depends on the particular case.

So, in some case the usage of JSON construct could be very helpful when you for example assignee some property of object so you can do this in the Json file and then interpret this file in you js code. To read a json file you can use some construct like this:

 

//Global Variables
var pjson ="mfg.json";
var jsonData;

gotJSON=function(data){
 
  console.log("gotJSON:Working on DATA >>>>"+data);
  console.warn(data);
  try{jsonData=JSON.parse(data);} catch(ex){console.log('exception');console.warn(ex);}
  $scope.view.wdg.JSONList.list=jsonData.Steps;
  $scope.view.wdg.JSONList.label='Title';
  $scope.view.wdg.JSONList.value=jsonData.Steps[0].Title;
 }

doRead=function (jsonFile){
fetch(jsonFile)
  .then(response=>response.text())
  .then(data=>gotJSON(data))
}

$scope.Init=function() {
doRead('./app/resources/Uploaded/'+pjson);
}
/////
$scope.$on('$ionicView.afterEnter',function(){
$scope.Init();})

 

Depending on this what your json file implements then you can do different things -.  for e.g. setting multiple property:

 

$scope.app.JsonTest= function(){
//store in a global variabe some json properties for testing
//
var b={"chuck":{"visible":false,"shader":""},"chuckHolder":{"visible":false,"opacity":"1"},"chipHolder":{"visible":false,"opacity":"1"},
       "chipScrew":{"visible":false,"shader":""},"chipBolt":{"visible":false,"opacity":"1"},"estop":{"visible":false,"opacity":"1"},
       "frontDecal":{"visible":false,"shader":""},"spindleDecal":{"visible":false,"opacity":"1"},"estopFront":{"visible":false,"opacity":"1"}
      }  
$scope.JsonTestList=b;
$scope.setMutipleProps($scope.JsonTestList);  

}
//======= definition of set multiple props
$scope.setMutipleProps= function(obj){
Object.keys(obj).forEach(function (item){
            for(var prop in obj[item])
            						{                   
                   console.log("obj["+item+"]["+prop+"]="+obj[item][prop] )  
                  $scope.view.wdg[item][prop]=obj[item][prop]                   
                   console.log("==>$scope.view.wdg["+item+"]["+prop+"]="+obj[item][prop] )                  }
										})
                         
};

 

This and  a lot of other things  - but as already mentioned above this depend on your application case but also on your imagination, creativity and last but not least on your javascript skills.

 

 

Top Tags