在视图中创建新集合

编程入门 行业动态 更新时间:2024-10-26 19:37:11
视图中创建新集合 - Backbone.js(Creating new Collection in view - Backbone.js)

我正在编写一个简单的Backbone.js代码,它基于从JSON获取的配置创建练习。

流程现在是:

我的代码中的某种控制器获取JSON文件(通过jQuery)。 我通过迭代JSON文件中'answers'数组中的所有项目并将它们添加到集合中来创建描述模型(基于我的JSON文件的一部分)和答案集合。 我根据模型和集合创建视图。

所有这些都在本主题中进行了描述和解决

这是我的JSON文件的一部分,它是练习的配置。

"config": { "id": "myWomiExercise1", "type": "", "numberOfSets": 0, "numberOfPresentedAnswers": 3, "numberOfCorrectAnswerInSet": 1, "randomAnswers": true, "exerciseTrueFalseType": false },

这意味着,我可以在我的JSON中拥有30个答案,并且随机选择(通过使用下划线方法),以显示3(哪一个是正确的)。

我将配置传递给新的View,并通过添加到视图来访问它

initialize: function(options) { this.options = options || {}; }

而且,我需要创建,因为我的配置说(“randomAnswers”:true)新的答案集。 正如谷歌所说,我可能需要使用下划线方法创建一个新的集合,通过组合其中的一些 - 因为我需要的是,创建的集合将提供x个答案, y将是正确的, xy不正确,以及最终,随机。 由于链接不适用于'where',我想知道这样做的最佳方法是什么? 我的想法,看起来很差,在视图中有这个:

randomAnswerSet: function(config) { var correct = config.numberOfCorrectAnswerInSet; var wrong = config.numberOfPresentedAnswers - correct; var set = []; set.push( _.sample( this.collection.getCorrect(), correct ) ); set.push( _.sample( this.collection.getWrong(), wrong ) ); this.collection = new AnswersCollection(_.shuffle(_.flatten(set))); },

还有一个问题,我应该创建一个新的集合,比如View中的randomAnswerSet,而不是正常渲染这个视图? 或者有更好的方法来做到这一点?

它让我困惑,因为我需要一个控制按钮'新例子',它将重新创建集合(因为一切都是随机的,它应该提供完整的新集),但是我需要销毁旧的吗? 用户点击按钮10次会有很多收集,是否最佳?

谢谢大家,祝大家有个美好的一天!

I'm writing a simple Backbone.js code, that create exercise based on config fetched from JSON.

The flow is now:

Some kind of controller in my code get JSON file (by jQuery). I create description model (based on part of my JSON file), and an answer collections by iterating through all items in 'answers' array in JSON file, and adding them to collection. I create views, based on model & collection.

All of this was described and resolved in this topic

That's part of my JSON file which is configuration for an exercise.

"config": { "id": "myWomiExercise1", "type": "", "numberOfSets": 0, "numberOfPresentedAnswers": 3, "numberOfCorrectAnswerInSet": 1, "randomAnswers": true, "exerciseTrueFalseType": false },

That means, that I can have for example 30 answers in my JSON, and there will be randomly selected (by using underscore methods, probably), to show 3 (which one is correct).

I pass configuration to new View, and have access to It by adding to the view

initialize: function(options) { this.options = options || {}; }

And than, I need to create, as my config says ("randomAnswers": true) new set of answers. As google said me, I probably need to create a new collection using underscore methods, by combining few of them - because what I need is, set created that will have x answers presented, from which y will be correct, and x-y incorrect, and finnaly, randomized. As chaining doesn't work with 'where', I'm wondering what is the best way of doing this? My idea, which looks pretty poor is having this in View:

randomAnswerSet: function(config) { var correct = config.numberOfCorrectAnswerInSet; var wrong = config.numberOfPresentedAnswers - correct; var set = []; set.push( _.sample( this.collection.getCorrect(), correct ) ); set.push( _.sample( this.collection.getWrong(), wrong ) ); this.collection = new AnswersCollection(_.shuffle(_.flatten(set))); },

And another question, should I create a new collection, like this randomAnswerSet in View and than render this view normally? Or there are better ways of doing this?

It confuses me, because I need a control button 'new example', which will recreate collection (as everything is randomized, It should gives completly new set), but do I need to destroy old one? There will be a lot of collection when user will click a button 10 times, is it optimal?

Thanks guys for all and have a great day!

最满意答案

首先,您确实意识到,如果用户点击“新示例”,他会立即知道正确答案,对吧? 它将是唯一一个显示两次的...

至于如何实现它,我将定义一个包含所有答案的新集合类型,并返回一个带有方法的集合。 你可以在这里看到类似的实现: https : //github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js (参见FilteredCollection),它在https中使用:/ /github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js

它在应用程序中的使用方式是过滤后的集合包含对原始联系人集合(所有数据)的引用,并在过滤时返回新集合。 在您的情况下,您只需要一个randomAnswerSet函数,它返回一组答案,而不是上面文件中的filter方法。

希望这可以帮助!

First of all, you do realize that if the user clicks "new example" he will immediately know the correct answer, right? It will be the only one that gets displayed twice...

As to how to implement it, I would define a new collection type which holds all the answers, and returns a set with a method. You can see a similar implementation here: https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/entities/common.js (see the FilteredCollection), which is used in https://github.com/davidsulc/marionette-gentle-introduction/blob/master/assets/js/apps/contacts/list/list_controller.js

The way it is used in the application is that the filtered collection hold a reference to the original collection of contacts (all the data), and returns a new collection when it gets filtered. In your case, you simply have a randomAnswerSet function that return a group of answers instead of the filtermethod in the files above.

Hope this helps!

更多推荐

本文发布于:2023-08-06 16:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454148.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   建新   中创

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!