0

我在我的应用程序加载的 js 文件中有这个视图模型:

var portfolioViewModel = function() {
    var self = this;
    this.selectedCompany = ko.observable('All Companies');
    this.allComp = ko.observable(true);
    this.chartSeries = ko.observableArray();
    $(function(){
        self.chart.addSeries(companyChart['All Companies']);
    });
    $.each(companyData, function(index, company) {
       self[company] = ko.observable(false);
       self.chartSeries.push(companyChart[company]);
   });


    this.chart = ko.observable();

    this.showCompany = function(company){
        self.hideCompanies();
        self[company](true);
        self.allComp(false);
        self.selectedCompany(company);
        while(self.chart.series.length > 0){
            self.chart.series[0].remove(true);
        }
        self.chart.addSeries(companyChart[company]);
    }
    this.allCompanies = function(){
        self.hideCompanies();
        self.allComp(true);
        self.selectedCompany('All Companies');
        self.chart.addSeries(companyChart['All Companies']);
        $.each(companyData, function(index, company) {
            self.chart.addSeries(companyChart[company]);
        });
    }

    this.hideCompanies = function(){
        $.each(companyData, function(i, c){
           self[c](false);
        });
        while(self.chart.series.length > 0){
            self.chart.series[0].remove(true);
        }
    }
}

它根据选择的任何公司控制将系列添加到高图表图表。

在我的部分中,我有一些 HTML,然后是以下 javascript 代码块:

<!--SCRIPTS-->
<script type="text/javascript">
    <% companies = current_user.list_of_companies %>
    <% funding_date = current_user.first_funding_date * 1000 %>
    var companyData = <%= companies.map(&:name).to_json.html_safe %>;
    var companyChart = {};

    companyChart['All Companies'] = {
                           name: 'Total Portfolio',
                           pointInterval: <%= 1.day * 1000 %>,
                           pointStart: <%=funding_date %>,
                           data: <%= current_user.portfolio_values.group("portfolio_values.day").select("portfolio_values.day, SUM(portfolio_values.value) as totals").map(&:totals).collect{|x| x.to_i} %>
                         }

    <% companies.each do |company|%>

    companyChart['<%= company.name %>'] = {
                          name: '<%= company.name %>',
                          pointInterval: <%= 1.day * 1000 %>,
                          pointStart: <%= funding_date %>,
                          data: <%= current_user.portfolio_values.where(:company_id => company.id).map(&:value).collect{|x| x.to_i} %>
                        }

    <% end %>

    var vm = new portfolioViewModel();
    ko.applyBindings(vm);


    vm.chart = new Highcharts.StockChart({
          chart: {
                renderTo: 'chart1',
                backgroundColor: 'transparent',
                zoomType: 'xy',
                type: 'areaspline',
                style: {
                    color: '#ffffff'
                }
             },
            labels : {
                style: {
                    color: 'red'
                }
            },
            colors: [
            '#ea00ff',
            '#229aff',
            '#ff4e00',
            '#ea00ff',
            '#229aff',
            '#ff4e00',
            '#ea00ff',
            '#229aff',
            '#ff4e00',
            '#ea00ff',
            '#229aff',
            '#ff4e00'
            ],
            credits: {
                enabled: false
            },
            rangeSelector: {
                enabled: false,
                buttons: [{
                    type: 'month',
                    count: 1,
                    text: '1m'
                }, {
                    type: 'ytd',
                    text: 'YTD'
                }, {
                    type: 'all',
                    text: 'All'
                }],
                buttonTheme: { // styles for the buttons
                            fill: 'none',
                            stroke: 'none',
                            style: {
                                color: '#fff',
                                fontWeight: 'bold'
                            },
                            states: {
                                hover: {
                                    stroke: 'none',
                                    fill: 'black'
                                },
                                select: {
                                    stroke: 'none',
                                    fill: 'black',
                                    style: {
                                        color: 'white'
                                    }
                                }
                            }
                        },
                        inputStyle: {
                            color: '#fff',
                            fontWeight: 'bold',
                            borderColor:'transparent',
                            background: 'transparent'

                        },
                        labelStyle: {
                            color: 'silver',
                            fontWeight: 'bold'
                        }
            },
            navigator: {
                enabled: false,
            },
            plotOptions : {
                areaspline : {
                    lineWidth : 2,
                    fillOpacity : .2,
                    shadow:true,
                    marker : {
                        enabled : false,
                        symbol: 'circle'
                    }
                }
            },
            yAxis: {
              alternateGridColor: 'rgba(0,0,0,0.1)',
              gridLineColor: 'rgba(0,0,0,0.3)',
              lineColor: 'rgba(0,0,0,0.3)',
              lineWidth: 1,
                labels: {
                    style: {
                        color: 'rgba(255,255,255,0.6)',
                        fontWeight: 'bold'
                    }
                }
            },
            xAxis: {
                gridLineWidth: 1,
                gridLineColor: 'rgba(0,0,0,0.3)',
                type: 'datetime',
                lineColor: 'rgba(0,0,0,0.3)',
                labels: {
                    style: {
                        color: 'rgba(255,255,255,0.6)',
                        fontWeight: 'bold'
                    }
                }
            },
            scrollbar : {
                enabled : false
            },
        series: vm.chartSeries()
      });

</script>
<!--SCRIPTS-->

有时会应用绑定并加载部分脚本,但很多时候它们不会。当他们没有通过对部分的远程调用加载时(即使 HTML 通过),脚本块似乎会完全丢失。

我什至不确定如何开始调试。有没有人看到任何明显的错误?我对javascript很陌生。

4

1 回答 1

0

通常,如果有任何类型的 javascript 错误停止执行以后的 javascript,这将阻止您的 ko 模型加载,或者导致它仅加载一部分。我建议将 Firebug 与 Firefox 或类似的东西一起使用,以检查导致问题的 javascript 错误。

此外,“gon”gem 是从 Rails 模型中获取信息到 javascript 中的好方法。我还喜欢将 rabl gem 与 .json.rabl 模板一起用于 ajax 请求以更新淘汰模型。

于 2013-02-05T19:04:27.800 回答