5

我正在尝试使用带有 ColdFusion 后端的 AngularJS,但遇到了一些障碍。我正在使用 CF Art Gallery 数据库修改他们的“待办事项”应用程序http://angularjs.org/ 。我正在尝试使用 AJAX 将 ColdFusion CFC 链接到 Angular 应用程序。

下面是我的艺术家.cfc:

<cfcomponent>

<cffunction name="getArtists" access="remote" >
    <cfargument name="firstName" default="">
    <cfargument name="lastName" default="">

    <cfquery name="getArtists_sql" datasource="cfartgallery">
        SELECT
            firstname as text,
            lastname as done 
        FROM artists
        WHERE 0=0
    <cfif firstName neq "">
        AND ucase(firstname) like ucase('%#FIRSTNAME#%')
    </cfif>
    <cfif lastName neq "">
        OR ucase(lastname) like ucase('%#LASTNAME#%')       
    </cfif>
    </cfquery>

    <cfreturn getArtists_sql>
</cffunction>

</cfcomponent>

我使用带有以下代码的 AngularJS 调用 CFC:

function TodoCtrl($scope, $http) {
    $http.get('cfc/artists.cfc?method=getArtists&returnformat=json').
        success(function (response) {
            $scope.todos = data.DATA;
    }).
        error(function (data) {
            $scope.todos = data;
        });
}

我知道我得到了回复。下面是 Chrome 的开发者工具为我返回的 JSON 字符串:

{
"COLUMNS":
    ["TEXT","DONE"],
"DATA":[
    ["Aiden","Donolan"],
    ["Austin","Weber"],
    ["Elicia","Kim"],
    ["Jeff","Baclawski"],
    ["Lori","Johnson"],
    ["Maxwell","Wilson"],
    ["Paul","Trani"],
    ["Raquel","Young"],
    ["Viata","Trenton"],
    ["Diane","Demo"],
    ["Anthony","Kunovic"],
    ["Ellery","Buntel"],
    ["Emma","Buntel"],
    ["Taylor Webb","Frazier"],
    ["Mike","Nimer"]
]}

这看起来不像他们演示中使用的符号 Angular:

[
{text:'learn angular', done:true},
{text:'build an angular app', done:false}
]

有人可以指出我如何才能让它正常工作的正确方向吗?理想情况下,我希望保持 CFC 完整,以便可以将其重用于不同的应用程序,因此 JSON 操作必须在 Javascript 端完成。

4

3 回答 3

7

默认情况下,Coldfusion 使用不同于您习惯的 JSON 表示法。列名存储在一个数组中,而数据存储在另一个数组中。我们实施的解决方案涉及将 CFquery 更改为数组。然后 JSONEncoding 该数组。

您将在这里需要此功能:

<cffunction name="QueryToArray" access="public" returntype="array" output="false"hint="This turns a query into an array of structures.">
    <cfargument name="Data" type="query" required="yes" />

    <cfscript>
        // Define the local scope.
        var LOCAL = StructNew();

        // Get the column names as an array.
        LOCAL.Columns = ListToArray( ARGUMENTS.Data.ColumnList );

        // Create an array that will hold the query equivalent.
        LOCAL.QueryArray = ArrayNew( 1 );

        // Loop over the query.
        for (LOCAL.RowIndex = 1 ; LOCAL.RowIndex LTE ARGUMENTS.Data.RecordCount ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){

        // Create a row structure.
        LOCAL.Row = StructNew();

        // Loop over the columns in this row.
        for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE ArrayLen( LOCAL.Columns ) ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){

        // Get a reference to the query column.
        LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];

        // Store the query cell value into the struct by key.
        LOCAL.Row[ LOCAL.ColumnName ] = ARGUMENTS.Data[ LOCAL.ColumnName ][ LOCAL.RowIndex ];

        }

        // Add the structure to the query array.
        ArrayAppend( LOCAL.QueryArray, LOCAL.Row );

        }

        // Return the array equivalent.
        return( LOCAL.QueryArray );

    </cfscript>
</cffunction>

然后您的回报将如下所示:

 <cfreturn SerializeJson(QueryToArray(getArtists_SQL),true)>

要记住的是,CFquery 对象包含其他属性,如记录计数......而且很可能,JS 只想要数据。我不知道是否有更优雅的解决方案,但这是我们在 JQgrid 遇到类似问题时采用的解决方案。

于 2013-02-25T18:25:37.053 回答
2

同意布莱斯的上述回答。我使用的 queryToArray 查看查询对象的 columnList。这样就保留了列别名的情况。否则,它将在您的 JSON 中全部大写

/**queryToArray
*  utility method to keep the code dry.
*  @hint does exactly what the name says, take a query, makes it an array of stucts
*  @hint columnLabels pass in a list of columnLabels to just return those columns
*/
public array function queryToArray(required query data, any columnLabels=false){
    var columns = listToArray(arguments.data.columnList);
    if(arguments.columnLabels != false){
            columns = listToArray(arguments.columnLabels);
    }

    var queryArray = arrayNew(1);

    for(i=1; i <= arguments.data.RecordCount; i++){

            row = StructNew();
            for (j=1; j <= ArrayLen(columns); j++){
                columnName = columns[j];
        row[columnName] = arguments.data[columnName][i];
            }
            arrayAppend(queryArray, row);
    }
    return(queryArray);
}
于 2013-08-21T02:25:58.627 回答
2

或者您可以在 javascript 中使用此辅助函数将查询作为(通用)键值对象数组。



    function CFQueryParser(data) {
        let items = [];
        Object.keys(data.DATA).forEach((i) => {
            let item = {};
            Object.keys(data.COLUMNS).forEach((j) => {
                item[data.COLUMNS[j]] = data.DATA[i][j];
            });
            items.push(item);
        })
        return items;
    }


于 2018-04-25T19:49:40.103 回答