To sum up CRM records, you may be able to use the aggregate function of the Fetch XML builder. When using the builder, you'll find this checkbox in the top left of the page. Checking that off will allow you to set an aggregate, such as sum, for your attributes.
Also have a look at our client side APIs to run your code as soon as the datasource is ready to be used. Check out the 'tpc:ready' event here: https://www.crmportalconnector.com/developer-network/documentation/portal-connector-widgets/tpc-client-side-api/mvc-based
Let me know if this helps, or if you need any further details.