A function cannot be called unless it was defined in the same file or one loaded before the attempt to call it.
A function cannot be called unless it is in the same or greater scope then the one trying to call it.
You declare function fn1 in first.js, and then in second you can just have fn1();
1.js :
function fn1 (){
alert();
}
2.js :
fn1();
index.html :
<script type="text/javascript" src="1.js"></script>
<script type="text/javascript" src="2.js"></script>
It works fine :)
I think you missed something here. In the context of a Blockchain environment, you must consider a method/function which writes data and a method which reads data in a completely different manner.
Read data (call):
That's really easy to read data on the Blockchain, you don't have to go too far, your node already contains the whole blockchain. It's kind of accessing the local storage.
In the Ethereum ecosystem, You will have to write a read-only function that doesn't modify the state (variables).
For example
Solidity
function getCampaignDetails(uint campaignID) constant returns (uint256, uint256) {
Campaign c = campaigns[campaignID];
return (c.fundingGoal, c.amount);
}
Web3
CrowdFunding.deployed().then(function(instance) {
return instance.getDetails.call(id);
}).then(function(details) {
console.log(details);
})
I use the keyword constant to enforce the compiler that's a read-only function (optional). And a Web3 call returns a promise that contains the result of the function (see doc).
A call doesn't cost any gas, can't receive ether, is synchronous and the return value of the contract function is returned immediately.
Write data (transaction):
To write data on the blockchain, you have to send a transaction, it's way more complicated because the transaction has to be broadcast on the network, mined, validated and published.
For example
Solidity
function newCampaign(address beneficiary, uint goal) {
campaignID = numCampaigns++;
campaigns[campaignID] = Campaign(beneficiary, goal, 0, 0);
}
Web3
CrowdFunding.deployed().then(function(instance) {
return instance.newCampaign(account_two, 2);
}).then(function (transactionHash) {
console.log(transactionHash); // useful for tracking
})
In this example, the function newCampaign changes the state by adding a new campaign to the map.
A transaction is asynchronous, the success of execution depends on the miner so you can't expect any data in return.
However, you can send an event to share some data.
So basically, your code can't work because you need this campaignID. I would store the campaign creator and the campaignID in the Campaign struct, build a call function that returns my campaigns (Filter using campaignCreator == msg.sender) and then select a campaign and run your test.
Best Answer
Not sure whether this is the intended way, but experimenting with bits and pieces of code found around the Internet resulted in something that seems to be working. Here's what I did in my javascript app:
The json file is what was produced by truffle when migrating MyContract. Note that I had to install
truffle-contract
(on top of truffle) for this to work. If anyone has a more elegant way of doing this, please share!