简体   繁体   English

从锂电池控制器获取的骨干网模型未正确加载到bb模型中

[英]Backbone Model fetched from Lithium controller is not loaded properly in bb Model

I'm using backbone.js and Lithium. 我正在使用ribs.js和Lithium。 I'm fetching a model from the server by passing in a _id that is received as a hidden parameter on the page. 我通过传递_id作为页面上的隐藏参数来从服务器中获取模型。

The database MongoDB has stored the data correctly and can be viewed from console as: 数据库MongoDB已经正确存储了数据,可以从控制台中以以下方式查看该数据库:

{ "_id" : ObjectId("50bb82694fbe3de417000001"), "holiday_name" : "SHREE15", "description": "", "star_rating" : "3", "holiday_type" : "family", "rooms" : "1", "adults" : "2", "child" :"0", "emails" : "" }

The Lithium Model class is so: 锂模型类是这样的:

class Holidays extends \lithium\data\Model {
public $validates = array(
        'holiday_name' => array(
                array(
                        'notEmpty',
                        'required' => true,
                        'message' => 'Please key-in a holiday name! (eg. Family trip for summer holidays)'
                ))); }

The backbone Holiday model is so: 骨干假日模型是这样的:

    window.app.IHoliday = Backbone.Model.extend({       
    urlRoot: HOLIDAY_URL,
    idAttribute: "_id",
    id: "_id",
    // Default attributes for the holiday.
    defaults: {
    },

    // Ensure that each todo created has `title`.
    initialize: function(props) {

    },

The code for backbone/fetch is: 骨干/获取的代码是:

var Holiday = new window.app.IHoliday({ _id: holiday_id });
        Holiday.fetch(
                {
                    success: function(){
                        alert('Holiday fetched:' + JSON.stringify(Holiday));
                        console.log('HOLIDAY Fetched: \n' + JSON.stringify(Holiday));
                        console.log('Holiday name:' + Holiday.get('holiday_name'));
                    }
                }               
           );

Lithium Controller Code is: 锂控制器代码为:

public function load($holiday_id)
{
    $Holiday = Holidays::find($holiday_id);
    return compact('Holiday');
}

PROBLEM: The output of the backbone model fetched from server is as below and the Holiday model is not correctly 'formed' when data returns into backbone Model: 问题:从服务器获取的骨干模型的输出如下,当数据返回到骨干模型中时,假日模型未正确“形成”:

    HOLIDAY Fetched: 
{"_id":"50bb82694fbe3de417000001","Holiday":{"_id":"50bb82694fbe3de417000001","holiday_name":"SHREE15","description":"","star_rating":"3","holiday_type":"family","rooms":"1","adults":"2","child":"0","emails":""}}

iplann...view.js (line 68)

Holiday name:undefined

Clearly there is some issue when the data is passed/translated from Lithium and loaded up as a model into backbone Holiday model. 显然,从锂传递/转换数据并将其作为模型加载到主链Holiday模型中时,存在一些问题。 Is there something very obviously wrong in my code? 我的代码中有什么很明显的错误吗?

By default, Backbone expects the model data at the root of the response. 默认情况下,Backbone期望模型数据位于响应的根。 You are putting it under the "Holiday" key. 您将其放在“假日”键下。 In your controller, try return $Holiday->to("array"); 在您的控制器中,尝试return $Holiday->to("array"); . That should return it as Backbone expects. 这应该按Backbone的预期返回。 Alternately, you can override your Backbone model's parse() method. 或者,您可以覆盖Backbone模型的parse()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM