SortProperties由于某种原因不起作用

时间:2013-11-05 08:00:30

标签: javascript ember.js

我有一个奇怪的问题,我正在尝试使用sortProperties按项目名称对我的模型进行排序,但它不起作用。

这是我的controller

App.Model = Ember.Object.extend({

});

//Map appropriate columns
App.Projects = App.Model.extend({
    id : null,
    projectname : null,
    projectdesc : null,
    projectbudget : null
});

App.ProjectsController = Ember.ObjectController.extend({

});

App.ProjectsController = Ember.ArrayController.extend({
    sortProperties: ['projectname'],
    sortAscending: true,
    //This function is binded to the searchText value of search box.
    filteredContent : function() {
        //'i' flag means user can search with either upper case or lower case character

        var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

        return this.get('model').filter(function(item) {

            return regex.test(item.projectname);
        });
    }.property('searchText', 'model')
});

App.ProjectdetailController = Ember.ObjectController.extend({
    actions : {
        update : function() {
            var id = this.get('id');
            data = {
                projectname : this.get('projectname'),
                projectdesc : this.get('projectdesc'),
                projectbudget : this.get('projectbudget'),
            };
            console.log(JSON.stringify(data));
            console.log(id);
            $.ajax({
                type : "POST",
                url : "http://pioneerdev.us/users/editProject/" + id,
                data : data,
                dataType : "json",
                success : function(data) {
                    console.log('success');
                    //alert('');
                }
            });
            alertify.success("Record Updated");
            this.transitionTo('projects');
            return false;
        }
    }
});

//Model to back the Projects template
App.ProjectsRoute = Ember.Route.extend({
    model : function() {
        return App.Projects.findAll();
    }
});

//Model to ProjectDetail view
App.ProjectdetailRoute = Ember.Route.extend({
    model : function(params) {
        return App.Projects.findBy(params.project_id);
    }
});

//methods for the model
App.Projects.reopenClass({
    sortProperties: ['projectname'],
    findAll : function() {
        return new Ember.RSVP.Promise(function(resolve, reject) {
            $.getJSON("http://pioneerdev.us/users/getProjects", function(data) {
                var result = data.projects.map(function(row) {
                    return App.Projects.create(row);
                });
                resolve(result);
            }).fail(reject);
        });
    },

    findBy : function(project_id) {

        return new Ember.RSVP.Promise(function(resolve, reject) {
            var project = App.Projects.create();
            $.getJSON("http://ankur.local/users/getProjectById/" + project_id, function(data) {
                var result = project.setProperties(data.project);
                resolve(result);
            }).fail(reject);
        });
    }
});

这是模板:

<script type="text/x-handlebars" id="projects">

        <div class="row-fluid">
        <div class="span3 offset3">
            <div class="row-fluid">
                <div class="span10" id="projectlist">
                    {{input type="text" value=searchText placeholder="Search Your Projects"}}
                    <ul>
                        <li>{{#each item in filteredContent}}
                            {{#link-to 'projectdetail' item}}{{item.projectname}}{{/link-to}}
                            <br>
                            {{else}}
                            <p>Sorry, No Data</p>
                            {{/each}}
                        </li>

                    </ul>
                </div>
            </div>

            <div class="row">
                <div class="span5 offset5">{{outlet}}</div>
            </div>
        </div>

        </div>

我能做些什么来实现我的动机?我的代码有什么不对吗?

1 个答案:

答案 0 :(得分:1)

在您的filteredContent CP中,您应该过滤数组控制器的arrangeContent属性

http://emberjs.com/api/classes/Ember.ArrayController.html#property_arrangedContent

App.ProjectsController = Ember.ArrayController.extend({
    sortProperties: ['projectname'],
    sortAscending: true,
    //This function is binded to the searchText value of search box.
    filteredContent : function() {
        //'i' flag means user can search with either upper case or lower case character

        var searchText = this.get('searchText'), regex = new RegExp(searchText, 'i');

        return this.get('arrangedContent').filter(function(item) {

            return regex.test(item.projectname);
        });
    }.property('searchText', 'arrangedContent')
});